Assets/Base.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 100% 72 / 72 CC 11 SCAR 11.3
CC
11
Cov
100%
prepare_assets_refs
6.0
format_extensions_list
3.0
find_files
2.0
CRAP 3.1
« Assets/Output.pm ↑ Summary Assets.pm »
1 5 package Rails::Assets::Base {
2 5 ▶ use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶ use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.02';
8 5 ▶ use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our @EXPORT = qw(
10 find_files
11 prepare_extensions_refs
12 prepare_assets_refs
13 );
14  
15 6 ▶ sub find_files {
Subroutine find_files: called CC=2 SCAR=6.9
Pod documented
16 6 my $dirs = shift;
17 6 ▶ die "Invalid reference provided. Expected ARRAY of directories: $!" unless (scalar @{$dirs} > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$dirs;} > 0 1 5 6
18 5 my @found;
19 5 ▶ my $wanted = sub { push @found, $File::Find::name if -f };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 52 45 97
20 5 find({ wanted => $wanted, no_chdir=> 1}, @$dirs);
21 5 return [sort {"\L$a" cmp "\L$b"} @found];
22 }
23  
24 7 ▶ sub prepare_extensions_refs {
Subroutine prepare_extensions_refs: called CC=2 SCAR=6.9
Pod documented
25 7 my ($extensions) = @_;
26 7 my $extensions_keys = format_extensions_list($extensions);
27 7 my ($assets);
28 7 $assets->{$_} = [()] foreach (@$extensions_keys);
29 7 return $assets;
30 }
31  
32 3 ▶ sub prepare_assets_refs {
Subroutine prepare_assets_refs: called CC=6 SCAR=17.9
Pod documented
33 3 my ($dirs, $extensions) = @_;
34 3 my $extensions_keys = format_extensions_list($extensions);
35 3 my $assets = prepare_extensions_refs($extensions_keys);
36 3 my ($assets_path, $reversed_ext);
37 3 foreach my $d (@$dirs){
38 9 ▶ unless ($d =~ /public/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d =~ /public/) 6 3 9
39 6 push @$assets_path, "$d$_/" foreach (qw(fonts javascripts stylesheets));
40 }
41 9 push @$assets_path, $d;
42 }
43 3 foreach my $key (@$extensions_keys){
44 12 $reversed_ext->{$_} = $key foreach (@{$extensions->{$key}});
45 }
46 3 return ($assets, $assets_path, $reversed_ext);
47 }
48  
49 13 ▶ sub format_extensions_list {
Subroutine format_extensions_list: called CC=3 SCAR=11.0
Pod documented
50 13 my ($extensions) = @_;
51 13 ▶ return [(sort keys %$extensions)] if (ref($extensions) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $extensions eq "HASH" 8 5 13
52 5 ▶ return $extensions if(ref($extensions) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $extensions eq "ARRAY" 4 1 5
53 1 die "Invalid extension argument provided: $!";
54 }
55  
56 }
57  
58 =head1 NAME
59  
60 Rails::Assets::Base - Files Finder and Custom Data Structures for Rails::Assets.
61  
62 =head1 VERSION
63  
64 Version 0.02
65  
66 =head1 SYNOPSIS
67  
68 This module provide some utilities functions
69  
70 use Rails::Assets::Base;
71  
72 my $template_hash = prepare_extensions_refs($assets_extensions);
73 my ($assets_hash, $assets_paths, $reversed_ext) =
74 prepare_assets_refs($assets_directories, $assets_extensions);
75 ...
76  
77 =head1 EXPORT
78  
79 =head2 prepare_extensions_refs
80  
81 Uses L<format_extensions_list|"#format_extensions_list"> to find the assets types and returns the following data structure:
82  
83 my $expected_extensions_refs = {
84 fonts => [()],
85 images => [()],
86 javascripts => [()],
87 stylesheets => [()],
88 };
89  
90 =head2 prepare_assets_refs
91  
92 Returns C<($assets, $assets_path, $reversed_ext)> where :
93  
94 =over 3
95  
96 =item * C<$assets> is an Hash reference as L<prepare_extensions_refs|"#prepare_extensions_refs">
97  
98 =item * C<$assets_path> is Array reference containing C<$Rails::Assets::ASSETS_DIR> and their subfolders named as C<$assets> keys
99  
100 =item * C<$reversed_ext> is an Hash reference created reversing key value of C<$assets>
101  
102 =back
103  
104 =head2 find_files
105  
106 Takes an array reference of directories as argument and returns a sorted list of files for all subfolder
107  
108 =head1 SUBROUTINES/METHODS
109  
110 =head2 format_extensions_list
111  
112 This subroutine takes as argument an C<$assets_extensions> reference and returns an array reference of assets types
113  
114 my $assets_extensions = {
115 fonts => [qw(.ttf)],
116 images => [qw(.png)],
117 javascripts => [qw(.js)],
118 stylesheets => [qw(.css)],
119 };
120  
121 my $assets_ext = [qw(fonts images javascripts stylesheets)];
122 is_deeply(
123 Rails::Assets::Base::format_extensions_list($assets_extensions),
124 $assets_ext, 'format_extensions_list() works with an HASH reference'
125 );
126 is_deeply(
127 Rails::Assets::Base::format_extensions_list($assets_ext),
128 $assets_ext, 'format_extensions_list() works with an ARRAY reference'
129 );
130  
131 my $invalid_ref = sub { return 1 };
132 eval { Rails::Assets::Base::format_extensions_list($invalid_ref) } or my $at = $@;
133 like(
134 $at, qr/Invalid extension argument provided/,
135 'format_extensions_list() dies with a message when invalid reference provided'
136 );
137  
138 =head1 AUTHOR
139  
140 Mauro Berlanda, C<< <kupta at cpan.org> >>
141  
142 =head1 BUGS
143  
144 Please report any bugs or feature requests to C<bug-rails-assets at rt.cpan.org>, or through
145 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rails-Assets>. I will be notified, and then you'll
146 automatically be notified of progress on your bug as I make changes.
147  
148 Pull Requests, Issues, Stars and Forks on the project L<github repository|https://github.com/mberlanda/rails-assets-coverage> are welcome!
149  
150 =head1 SUPPORT
151  
152 You can find documentation for this module with the perldoc command.
153  
154 perldoc Rails::Assets::Base
155  
156 You can also look for information at:
157  
158 =over 4
159  
160 =item * RT: CPAN's request tracker (report bugs here)
161  
162 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
163  
164 =item * AnnoCPAN: Annotated CPAN documentation
165  
166 L<http://annocpan.org/dist/.>
167  
168 =item * CPAN Ratings
169  
170 L<http://cpanratings.perl.org/d/.>
171  
172 =item * Search CPAN
173  
174 L<http://search.cpan.org/dist/./>
175  
176 =back
177  
178 =head1 ACKNOWLEDGEMENTS
179  
180 =head1 LICENSE AND COPYRIGHT
181  
182 Copyright 2017 Mauro Berlanda.
183  
184 This program is free software; you can redistribute it and/or modify it
185 under the terms of the the Artistic License (2.0). You may obtain a
186 copy of the full license at:
187  
188 L<http://www.perlfoundation.org/artistic_license_2_0>
189  
190 Any use, modification, and distribution of the Standard or Modified
191 Versions is governed by this Artistic License. By using, modifying or
192 distributing the Package, you accept this license. Do not use, modify,
193 or distribute the Package, if you do not accept this license.
194  
195 If your Modified Version has been derived from a Modified Version made
196 by someone other than you, you are nevertheless required to ensure that
197 your Modified Version complies with the requirements of this license.
198  
199 This license does not grant you the right to use any trademark, service
200 mark, tradename, or logo of the Copyright Holder.
201  
202 This license includes the non-exclusive, worldwide, free-of-charge
203 patent license to make, have made, use, offer to sell, sell, import and
204 otherwise transfer the Package with respect to any patent claims
205 licensable by the Copyright Holder that are necessarily infringed by the
206 Package. If you institute patent litigation (including a cross-claim or
207 counterclaim) against any party alleging that the Package constitutes
208 direct or contributory patent infringement, then this Artistic License
209 to you shall terminate on the date that such litigation is filed.
210  
211 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
212 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
213 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
214 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
215 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
216 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
217 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
218 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
219  
220 =cut
221  
222 5 1; # End of Rails::Assets::Base
« Assets/Output.pm ↑ Summary Assets.pm »