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