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