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.037;
2             # ABSTRACT: a file whose content is (re-)built on demand
3              
4 18     18   5275 use Moose;
  18         46  
  18         155  
5              
6 18     18   139952 use Dist::Zilla::Pragmas;
  18         43  
  18         155  
7              
8 18     18   136 use Moose::Util::TypeConstraints;
  18         40  
  18         220  
9              
10 18     18   43086 use namespace::autoclean;
  18         48  
  18         194  
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   61 my ($self) = @_;
59 27 100       891 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 174 my ($self) = @_;
68              
69 4 100       331 confess("cannot set content of a FromCode file") if @_ > 1;
70              
71 3         150 my $code = $self->code;
72 3         12 my $result = $self->$code;
73              
74 3 100       96 if ( $self->code_return_type eq 'text' ) {
75 1         5 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 2397 my ($self) = @_;
88              
89 41 100       475 confess( "cannot set encoded_content of a FromCode file" ) if @_ > 1;
90              
91 40         1861 my $code = $self->code;
92 40         264 my $result = $self->$code;
93              
94 40 100       2661 if ( $self->code_return_type eq 'bytes' ) {
95 14         154 return $result;
96             }
97             else {
98 26         168 $self->_encode($result);
99             }
100             }
101              
102             sub _set_added_by {
103 41     41   159 my ($self, $value) = @_;
104 41         2887 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.037
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
138             released in the last two to three years. (That is, if the most recently
139             released version is v5.40, then this module should work on both v5.40 and
140             v5.38.)
141              
142             Although it may work on older versions of perl, no guarantee is made that the
143             minimum required version will not be increased. The version may be increased
144             for any reason, and there is no promise that patches will be accepted to
145             lower the minimum required perl.
146              
147             =head1 ATTRIBUTES
148              
149             =head2 code_return_type
150              
151             'text' or 'bytes'
152              
153             =head2 encoding
154              
155             =head2 content
156              
157             =head2 encoded_content
158              
159             =head1 AUTHOR
160              
161             Ricardo SIGNES 😏 <cpan@semiotic.systems>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2026 by Ricardo SIGNES.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut