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.029;
2             # ABSTRACT: something that can act like a file with changeable contents
3              
4 52     52   36813 use Moose::Role;
  52         20842  
  52         542  
5              
6 52     52   292822 use Dist::Zilla::Pragmas;
  52         156  
  52         383  
7              
8 52     52   482 use Moose::Util::TypeConstraints;
  52         152  
  52         599  
9 52     52   143802 use MooseX::SetOnce;
  52         591224  
  52         1887  
10              
11 52     52   486 use namespace::autoclean;
  52         164  
  52         447  
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 20823 my $self = shift;
50 580 100       1697 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     16324 if ( $self->has_content || $self->_content_source eq 'content' ) {
54 276         8369 return $self->_content;
55             }
56             else {
57 175         698 return $self->_content($self->_decode($self->encoded_content));
58             }
59             }
60             else {
61 129         675 my ($pkg, $line) = $self->_caller_of('content');
62 129         5183 $self->_content_source('content');
63 129         661 $self->_push_added_by(sprintf("content set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line));
64 129         5108 $self->clear_encoded_content;
65 129         3829 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 26894 my $self = shift;
84 950 100       2693 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     39644 if ($self->has_encoded_content || $self->_content_source eq 'encoded_content') {
88 797         25339 return $self->_encoded_content;
89             }
90             else {
91 151         704 return $self->_encoded_content($self->_encode($self->content));
92             }
93             }
94 2         10 my ($pkg, $line) = $self->_caller_of('encoded_content');
95 2         93 $self->_content_source('encoded_content');
96 2         10 $self->_push_added_by(sprintf("encoded_content set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line));
97 2         76 $self->clear_content;
98 2         64 $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   1835 my ($self, $value) = @_;
110 740         22730 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.029
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 released
151             in the last two to three years. (That is, if the most recently released
152             version is v5.40, then this module should work on both v5.40 and v5.38.)
153              
154             Although it may work on older versions of perl, no guarantee is made that the
155             minimum required version will not be increased. The version may be increased
156             for any reason, and there is no promise that patches will be accepted to lower
157             the minimum required perl.
158              
159             =head1 ATTRIBUTES
160              
161             =head2 encoding
162              
163             Default is 'UTF-8'. Can only be set once.
164              
165             =head2 content
166              
167             =head2 encoded_content
168              
169             =head1 AUTHOR
170              
171             Ricardo SIGNES 😏 <cpan@semiotic.systems>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2022 by Ricardo SIGNES.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut