| 1 | package Dist::Zilla::Role::MutableFile 6.040; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: something that can act like a file with changeable contents | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 54 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 54 | ▶ | use Dist::Zilla::Pragmas; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 54 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 54 | ▶ | use MooseX::SetOnce; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 54 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 54 | with 'Dist::Zilla::Role::File'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub encoding; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 54 | 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 | 54 | 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 | 595 | ▶ | sub content { | ||||||||||||||||||||||||||
|
Subroutine
content: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 595 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | 595 | ▶ | if ( ! @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 465 | ▶ | if ( $self->has_content || $self->_content_source eq 'content' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_content or $self->_content_source eq "content"
MC/DC50%1/2
$self->has_content or $self->_content_source eq "content"
$self->has_content
$self->_content_source eq "content"
Truth table66%2/3
A or B
A= $self->has_contentB= $self->_content_source eq "content"
| |||||||||||||||||||||||||||||
| 54 | 284 | return $self->_content; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||
| 57 | 181 | return $self->_content($self->_decode($self->encoded_content)); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 130 | my ($pkg, $line) = $self->_caller_of('content'); | |||||||||||||||||||||||||||
| 62 | 130 | $self->_content_source('content'); | |||||||||||||||||||||||||||
| 63 | 130 | $self->_push_added_by(sprintf("content set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line)); | |||||||||||||||||||||||||||
| 64 | 130 | $self->clear_encoded_content; | |||||||||||||||||||||||||||
| 65 | 130 | return $self->_content(@_); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #pod =attr encoded_content | ||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||
| 71 | #pod =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 54 | 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 | 977 | ▶ | sub encoded_content { | ||||||||||||||||||||||||||
|
Subroutine
encoded_content: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 977 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 977 | ▶ | if ( ! @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 975 | ▶ | if ($self->has_encoded_content || $self->_content_source eq 'encoded_content') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_encoded_content or $self->_content_source eq "encoded_content"
MC/DC100%2/2
$self->has_encoded_content or $self->_content_source eq "encoded_content"
$self->has_encoded_content
$self->_content_source eq "encoded_content"
Truth table100%3/3
A or B
A= $self->has_encoded_contentB= $self->_content_source eq "encoded_content"
| |||||||||||||||||||||||||||||
| 88 | 821 | return $self->_encoded_content; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 154 | return $self->_encoded_content($self->_encode($self->content)); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 2 | my ($pkg, $line) = $self->_caller_of('encoded_content'); | |||||||||||||||||||||||||||
| 95 | 2 | $self->_content_source('encoded_content'); | |||||||||||||||||||||||||||
| 96 | 2 | $self->_push_added_by(sprintf("encoded_content set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line)); | |||||||||||||||||||||||||||
| 97 | 2 | $self->clear_content; | |||||||||||||||||||||||||||
| 98 | 2 | $self->_encoded_content(@_); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 54 | has _content_source => ( | |||||||||||||||||||||||||||
| 102 | is => 'rw', | ||||||||||||||||||||||||||||
| 103 | isa => enum([qw/content encoded_content/]), | ||||||||||||||||||||||||||||
| 104 | lazy => 1, | ||||||||||||||||||||||||||||
| 105 | builder => '_build_content_source', | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 762 | ▶ | sub _set_added_by { | ||||||||||||||||||||||||||
|
Subroutine
_set_added_by: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 762 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 110 | 762 | 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 | 54 | requires '_build_content'; | |||||||||||||||||||||||||||
| 119 | 54 | 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 | 54 | requires '_build_content_source'; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 54 | 1; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||