File Coverage

blib/lib/Dist/Zilla/File/FromCode.pm
Criterion Covered Total %
statement 30 30 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: a file whose content is (re-)built on demand
2              
3             use Moose;
4 18     18   811  
  18         40  
  18         135  
5             use Dist::Zilla::Pragmas;
6 18     18   116571  
  18         43  
  18         144  
7             use Moose::Util::TypeConstraints;
8 18     18   150  
  18         44  
  18         224  
9             use namespace::autoclean;
10 18     18   39909  
  18         49  
  18         132  
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This represents a file whose contents will be generated on demand from a
14             #pod callback or method name.
15             #pod
16             #pod It has one attribute, C<code>, which may be a method name (string) or a
17             #pod coderef. When the file's C<content> method is called, the code is used to
18             #pod generate the content. This content is I<not> cached. It is recomputed every
19             #pod time the content is requested.
20             #pod
21             #pod =cut
22              
23             with 'Dist::Zilla::Role::File';
24              
25             has code => (
26             is => 'rw',
27             isa => 'CodeRef|Str',
28             required => 1,
29             );
30              
31             #pod =attr code_return_type
32             #pod
33             #pod 'text' or 'bytes'
34             #pod
35             #pod =cut
36              
37             has code_return_type => (
38             is => 'ro',
39             isa => enum([ qw(text bytes) ]),
40             default => 'text',
41             );
42              
43             #pod =attr encoding
44             #pod
45             #pod =cut
46              
47             sub encoding;
48              
49             has encoding => (
50             is => 'ro',
51             isa => 'Str',
52             lazy => 1,
53             builder => "_build_encoding",
54             );
55              
56             my ($self) = @_;
57             return $self->code_return_type eq 'text' ? 'UTF-8' : 'bytes';
58 27     27   85 }
59 27 100       845  
60             #pod =attr content
61             #pod
62             #pod =cut
63              
64             my ($self) = @_;
65              
66             confess("cannot set content of a FromCode file") if @_ > 1;
67 4     4 1 137  
68             my $code = $self->code;
69 4 100       397 my $result = $self->$code;
70              
71 3         101 if ( $self->code_return_type eq 'text' ) {
72 3         10 return $result;
73             }
74 3 100       93 else {
75 1         7 $self->_decode($result);
76             }
77             }
78 2         11  
79             #pod =attr encoded_content
80             #pod
81             #pod =cut
82              
83             my ($self) = @_;
84              
85             confess( "cannot set encoded_content of a FromCode file" ) if @_ > 1;
86              
87 41     41 1 2468 my $code = $self->code;
88             my $result = $self->$code;
89 41 100       509  
90             if ( $self->code_return_type eq 'bytes' ) {
91 40         1487 return $result;
92 40         219 }
93             else {
94 40 100       2208 $self->_encode($result);
95 14         90 }
96             }
97              
98 26         149 my ($self, $value) = @_;
99             return $self->_push_added_by(sprintf("%s from coderef added by %s", $self->code_return_type, $value));
100             };
101              
102             __PACKAGE__->meta->make_immutable;
103 41     41   129 1;
104 41         1310  
105              
106             =pod
107              
108             =encoding UTF-8
109              
110             =head1 NAME
111              
112             Dist::Zilla::File::FromCode - a file whose content is (re-)built on demand
113              
114             =head1 VERSION
115              
116             version 6.028
117              
118             =head1 DESCRIPTION
119              
120             This represents a file whose contents will be generated on demand from a
121             callback or method name.
122              
123             It has one attribute, C<code>, which may be a method name (string) or a
124             coderef. When the file's C<content> method is called, the code is used to
125             generate the content. This content is I<not> cached. It is recomputed every
126             time the content is requested.
127              
128             =head1 PERL VERSION
129              
130             This module should work on any version of perl still receiving updates from
131             the Perl 5 Porters. This means it should work on any version of perl released
132             in the last two to three years. (That is, if the most recently released
133             version is v5.40, then this module should work on both v5.40 and v5.38.)
134              
135             Although it may work on older versions of perl, no guarantee is made that the
136             minimum required version will not be increased. The version may be increased
137             for any reason, and there is no promise that patches will be accepted to lower
138             the minimum required perl.
139              
140             =head1 ATTRIBUTES
141              
142             =head2 code_return_type
143              
144             'text' or 'bytes'
145              
146             =head2 encoding
147              
148             =head2 content
149              
150             =head2 encoded_content
151              
152             =head1 AUTHOR
153              
154             Ricardo SIGNES 😏 <cpan@semiotic.systems>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2022 by Ricardo SIGNES.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut