File Coverage

blib/lib/App/Embra/File.pm
Criterion Covered Total %
statement 63 71 88.7
branch 12 28 42.8
condition n/a
subroutine 21 22 95.4
pod n/a
total 96 121 79.3


line stmt bran cond sub pod time code
1 16     16   14909 use strict;
  16         28  
  16         588  
2 16     16   78 use warnings;
  16         21  
  16         830  
3              
4             package App::Embra::File;
5             $App::Embra::File::VERSION = '0.001'; # TRIAL
6             # ABSTRACT: a file from your site
7              
8 16     16   98 use File::Basename;
  16         19  
  16         1415  
9 16     16   9121 use File::Spec::Functions qw< canonpath >;
  16         13392  
  16         1204  
10 16     16   5836 use Method::Signatures;
  16         531371  
  16         136  
11 16     16   12873 use Moo;
  16         92878  
  16         128  
12              
13             # mostly Dist::Zilla::File::OnDisk
14              
15              
16 16 50   16   1407184 use overload fallback => 1,
  16 50   24   44  
  16 50       108  
  24         43  
  24         73  
  24         32  
  24         69  
  24         46  
  24         75  
  24         675  
17             '""' => method( $other, $swap ) { $self->name };
18              
19              
20             has 'name' => (
21             is => 'rw',
22             required => 1,
23             );
24              
25              
26             has 'content' => (
27             is => 'rw',
28             lazy => 1,
29             default => method { $self->_read_file },
30             );
31              
32              
33             has 'mode' => (
34             is => 'rw',
35             lazy => 1,
36             default => method { oct(644) },
37             );
38              
39              
40             has '_original_name' => (
41             is => 'ro',
42             init_arg => undef,
43             );
44              
45              
46             has 'notes' => (
47             is => 'ro',
48             default => method { {} },
49             );
50 16 50   16   32362  
  14     14   18  
  14         35  
51 14         784  
52             has 'ext' => (
53             is => 'rw',
54 16 50   16   9972 lazy => 1,
  11     11   2419  
  11         44  
55 11         35 builder => 1,
56             trigger => 1,
57             );
58              
59 16 50   16   35648 method _split_name {
  3 50   3   11  
  3         10  
  3         4  
  3         17  
60 3         8 fileparse( $self->name, qr{ (?<= [.] ) [^.]+ $ }x );
61 3         9 }
62 3 50       17  
63 3         36 method _build_ext {
64             ($self->_split_name)[2];
65             }
66 16 50   16   34175  
  1 50   1   10  
  1         3  
  1         2  
  1         3  
67 1         16  
68             method with_ext( $ext ) {
69             $ext =~ s/ \A [.] //xms;
70 16 50   16   33690 my ($f, $d, $e) = $self->_split_name;
  34 50   34   204  
  34         127  
  34         52  
  34         117  
71 34         809 return $self->name if $e eq $ext;
72             return canonpath( $d . $f . $ext );
73             }
74 16 0   16   10580  
  0     0   0  
  0         0  
75 0         0 method _trigger_ext( $old_ext ) {
76 0 0       0 $self->name( $self->with_ext( $self->ext ) );
77             }
78 0         0  
79             method BUILD( $args ) {
80 0         0 $self->{_original_name} = $self->name;
  0         0  
  0         0  
81             }
82              
83             method _read_file {
84 16     16   33819 my $fname = $self->_original_name;
  3     3   1628  
  3         15  
85 3         8 open my $fh, '<', $fname or die "can't open $fname for reading: $!";
  3         38  
86              
87             binmode $fh, ':raw';
88              
89             my $content = do { local $/; <$fh> };
90             }
91              
92              
93             method update_notes( %notes ) {
94             @{ $self->notes }{ keys %notes } = values %notes;
95             }
96              
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             App::Embra::File - a file from your site
108              
109             =head1 VERSION
110              
111             version 0.001
112              
113             =head1 DESCRIPTION
114              
115             This represents a file to be included in your site.
116              
117             =head1 ATTRIBUTES
118              
119             =head2 name
120              
121             The name of the file. Change this to change where the file will appear in the site.
122              
123             =head2 content
124              
125             The content of the file. Change this to change the content of the file when it appears in the site. Defaults to the contents of C<_original_name>.
126              
127             =head2 mode
128              
129             The permissions of the file. Defaults to 0644.
130              
131             =head2 _original_name
132              
133             The original name of this file. This is automatically saved from the C<name> attributes used to construct the object, and can't be altered.
134              
135             =head2 notes
136              
137             A hash ref which stores extra values associated with the file. Transform plugins will read and write notes, and Assemble plugins will read notes.
138              
139             =head2 ext
140              
141             The extention of the file's C<name>. Changing this will cause the file's C<name> to be updated to match.
142              
143             =head1 METHODS
144              
145             =head2 with_ext
146              
147             $file->with_ext( $ext );
148              
149             Returns file's name with its extension changed to <$ext>.
150              
151             =head2 update_notes
152              
153             $file->update_notes( %more_notes );
154              
155             Merges C<%more_notes> into the file's existing notes.
156              
157             =head1 AUTHOR
158              
159             Daniel Holz <dgholz@gmail.com>
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is copyright (c) 2015 by Daniel Holz.
164              
165             This is free software; you can redistribute it and/or modify it under
166             the same terms as the Perl 5 programming language system itself.
167              
168             =cut