File Coverage

blib/lib/Rails/Assets/Base.pm
Criterion Covered Total %
statement 44 44 100.0
branch 10 10 100.0
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 68 68 100.0


line stmt bran cond sub pod time code
1             package Rails::Assets::Base {
2 5     5   138793   use 5.006;
  5         19  
3 5     5   27   use strict;
  5         7  
  5         86  
4 5     5   21   use warnings;
  5         8  
  5         121  
5 5     5   28   use File::Find;
  5         11  
  5         359  
6              
7               our $VERSION = '0.02';
8 5     5   35   use Exporter qw(import);
  5         9  
  5         2195  
9               our @EXPORT = qw(
10             find_files
11             prepare_extensions_refs
12             prepare_assets_refs
13             );
14              
15               sub find_files {
16 6     6 1 3093     my $dirs = shift;
17 6 100       18     die "Invalid reference provided. Expected ARRAY of directories: $!" unless (scalar @{$dirs} > 0);
  6         39  
18 5         12     my @found;
19 5 100   97   30     my $wanted = sub { push @found, $File::Find::name if -f };
  97         4632  
20 5         460     find({ wanted => $wanted, no_chdir=> 1}, @$dirs);
21 5         38     return [sort {"\L$a" cmp "\L$b"} @found];
  121         456  
22               }
23              
24               sub prepare_extensions_refs {
25 7     7 1 634     my ($extensions) = @_;
26 7         15     my $extensions_keys = format_extensions_list($extensions);
27 7         29     my ($assets);
28 7         33     $assets->{$_} = [()] foreach (@$extensions_keys);
29 7         20     return $assets;
30               }
31              
32               sub prepare_assets_refs {
33 3     3 1 1534     my ($dirs, $extensions) = @_;
34 3         13     my $extensions_keys = format_extensions_list($extensions);
35 3         13     my $assets = prepare_extensions_refs($extensions_keys);
36 3         8     my ($assets_path, $reversed_ext);
37 3         10     foreach my $d (@$dirs){
38 9 100       30       unless ($d =~ /public/) {
39 6         44         push @$assets_path, "$d$_/" foreach (qw(fonts javascripts stylesheets));
40                   }
41 9         24       push @$assets_path, $d;
42                 }
43 3         9     foreach my $key (@$extensions_keys){
44 12         21       $reversed_ext->{$_} = $key foreach (@{$extensions->{$key}});
  12         62  
45                 }
46 3         16     return ($assets, $assets_path, $reversed_ext);
47               }
48              
49               sub format_extensions_list {
50 13     13 1 1663     my ($extensions) = @_;
51 13 100       87     return [(sort keys %$extensions)] if (ref($extensions) eq 'HASH');
52 5 100       23     return $extensions if(ref($extensions) eq 'ARRAY');
53 1         9     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             1; # End of Rails::Assets::Base
223