File Coverage

blib/lib/Dist/Zilla/Role/MutableFile.pm
Criterion Covered Total %
statement 37 37 100.0
branch 8 8 100.0
condition 5 6 83.3
subroutine 8 8 100.0
pod 2 2 100.0
total 60 61 98.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Role::MutableFile 6.037;
2             # ABSTRACT: something that can act like a file with changeable contents
3              
4 52     52   41314 use Moose::Role;
  52         19106  
  52         568  
5              
6 52     52   296181 use Dist::Zilla::Pragmas;
  52         128  
  52         436  
7              
8 52     52   418 use Moose::Util::TypeConstraints;
  52         115  
  52         547  
9 52     52   140137 use MooseX::SetOnce;
  52         627189  
  52         3757  
10              
11 52     52   860 use namespace::autoclean;
  52         247  
  52         493  
12              
13             #pod =head1 DESCRIPTION
14             #pod
15             #pod This role describes a file whose contents may be modified
16             #pod
17             #pod =attr encoding
18             #pod
19             #pod Default is 'UTF-8'. Can only be set once.
20             #pod
21             #pod =cut
22              
23             with 'Dist::Zilla::Role::File';
24              
25             sub encoding;
26              
27             has encoding => (
28             is => 'rw',
29             isa => 'Str',
30             lazy => 1,
31             default => 'UTF-8',
32             traits => [ qw(SetOnce) ],
33             );
34              
35             #pod =attr content
36             #pod
37             #pod =cut
38              
39             has _content => (
40             is => 'rw',
41             isa => 'Str',
42             lazy => 1,
43             builder => '_build_content',
44             clearer => 'clear_content',
45             predicate => 'has_content',
46             );
47              
48             sub content {
49 580     580 1 18076 my $self = shift;
50 580 100       1683 if ( ! @_ ) {
51             # if we have it or we're tasked to provide it, return it (possibly lazily
52             # generated from a builder); otherwise, get it from the encoded_content
53 451 100 66     19806 if ( $self->has_content || $self->_content_source eq 'content' ) {
54 276         9679 return $self->_content;
55             }
56             else {
57 175         668 return $self->_content($self->_decode($self->encoded_content));
58             }
59             }
60             else {
61 129         674 my ($pkg, $line) = $self->_caller_of('content');
62 129         6258 $self->_content_source('content');
63 129         649 $self->_push_added_by(sprintf("content set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line));
64 129         5706 $self->clear_encoded_content;
65 129         4158 return $self->_content(@_);
66             }
67             }
68              
69             #pod =attr encoded_content
70             #pod
71             #pod =cut
72              
73             has _encoded_content => (
74             is => 'rw',
75             isa => 'Str',
76             lazy => 1,
77             builder => '_build_encoded_content',
78             clearer => 'clear_encoded_content',
79             predicate => 'has_encoded_content',
80             );
81              
82             sub encoded_content {
83 950     950 1 26747 my $self = shift;
84 950 100       2732 if ( ! @_ ) {
85             # if we have it or we're tasked to provide it, return it (possibly lazily
86             # generated from a builder); otherwise, get it from the content
87 948 100 100     49237 if ($self->has_encoded_content || $self->_content_source eq 'encoded_content') {
88 797         28539 return $self->_encoded_content;
89             }
90             else {
91 151         584 return $self->_encoded_content($self->_encode($self->content));
92             }
93             }
94 2         10 my ($pkg, $line) = $self->_caller_of('encoded_content');
95 2         88 $self->_content_source('encoded_content');
96 2         6 $self->_push_added_by(sprintf("encoded_content set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line));
97 2         52 $self->clear_content;
98 2         42 $self->_encoded_content(@_);
99             }
100              
101             has _content_source => (
102             is => 'rw',
103             isa => enum([qw/content encoded_content/]),
104             lazy => 1,
105             builder => '_build_content_source',
106             );
107              
108             sub _set_added_by {
109 740     740   1786 my ($self, $value) = @_;
110 740         26241 return $self->_push_added_by(sprintf("%s added by %s", $self->_content_source, $value));
111             };
112              
113             # we really only need one of these and only if _content or _encoded_content
114             # isn't provided, but roles can't do that, so we'll insist on both just in case
115             # and let classes provide stubs if they provide _content or _encoded_content
116             # another way
117              
118             requires '_build_content';
119             requires '_build_encoded_content';
120              
121             # we need to know the content source so we know where we might need to rely on
122             # lazy loading to give us content. It should be set by the class if there is a
123             # class-wide default or just stubbed if a BUILD modifier sets it per-object.
124              
125             requires '_build_content_source';
126              
127             1;
128              
129             __END__
130              
131             =pod
132              
133             =encoding UTF-8
134              
135             =head1 NAME
136              
137             Dist::Zilla::Role::MutableFile - something that can act like a file with changeable contents
138              
139             =head1 VERSION
140              
141             version 6.037
142              
143             =head1 DESCRIPTION
144              
145             This role describes a file whose contents may be modified
146              
147             =head1 PERL VERSION
148              
149             This module should work on any version of perl still receiving updates from
150             the Perl 5 Porters. This means it should work on any version of perl
151             released in the last two to three years. (That is, if the most recently
152             released version is v5.40, then this module should work on both v5.40 and
153             v5.38.)
154              
155             Although it may work on older versions of perl, no guarantee is made that the
156             minimum required version will not be increased. The version may be increased
157             for any reason, and there is no promise that patches will be accepted to
158             lower the minimum required perl.
159              
160             =head1 ATTRIBUTES
161              
162             =head2 encoding
163              
164             Default is 'UTF-8'. Can only be set once.
165              
166             =head2 content
167              
168             =head2 encoded_content
169              
170             =head1 AUTHOR
171              
172             Ricardo SIGNES 😏 <cpan@semiotic.systems>
173              
174             =head1 COPYRIGHT AND LICENSE
175              
176             This software is copyright (c) 2026 by Ricardo SIGNES.
177              
178             This is free software; you can redistribute it and/or modify it under
179             the same terms as the Perl 5 programming language system itself.
180              
181             =cut