| 1 | package Dist::Zilla::File::FromCode 6.040; | ||||||||||
| 2 | # ABSTRACT: a file whose content is (re-)built on demand | ||||||||||
| 3 | |||||||||||
| 4 | 18 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 18 | ▶ | use Dist::Zilla::Pragmas; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 18 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 18 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 18 | with 'Dist::Zilla::Role::File'; | |||||||||
| 25 | |||||||||||
| 26 | 18 | 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 | 18 | 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 | 18 | has encoding => ( | |||||||||
| 51 | is => 'ro', | ||||||||||
| 52 | isa => 'Str', | ||||||||||
| 53 | lazy => 1, | ||||||||||
| 54 | builder => "_build_encoding", | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | 27 | ▶ | sub _build_encoding { | ||||||||
|
Subroutine
_build_encoding: called CC=2 SCAR=6.9
| |||||||||||
| 58 | 27 | my ($self) = @_; | |||||||||
| 59 | 27 | ▶ | return $self->code_return_type eq 'text' ? 'UTF-8' : 'bytes'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | #pod =attr content | ||||||||||
| 63 | #pod | ||||||||||
| 64 | #pod =cut | ||||||||||
| 65 | |||||||||||
| 66 | 4 | ▶ | sub content { | ||||||||
|
Subroutine
content: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 67 | 4 | my ($self) = @_; | |||||||||
| 68 | |||||||||||
| 69 | 4 | ▶ | confess("cannot set content of a FromCode file") if @_ > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 3 | my $code = $self->code; | |||||||||
| 72 | 3 | my $result = $self->$code; | |||||||||
| 73 | |||||||||||
| 74 | 3 | ▶ | if ( $self->code_return_type eq 'text' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 1 | return $result; | |||||||||
| 76 | } | ||||||||||
| 77 | else { | ||||||||||
| 78 | 2 | $self->_decode($result); | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | #pod =attr encoded_content | ||||||||||
| 83 | #pod | ||||||||||
| 84 | #pod =cut | ||||||||||
| 85 | |||||||||||
| 86 | 41 | ▶ | sub encoded_content { | ||||||||
|
Subroutine
encoded_content: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 87 | 41 | my ($self) = @_; | |||||||||
| 88 | |||||||||||
| 89 | 41 | ▶ | confess( "cannot set encoded_content of a FromCode file" ) if @_ > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 40 | my $code = $self->code; | |||||||||
| 92 | 40 | my $result = $self->$code; | |||||||||
| 93 | |||||||||||
| 94 | 40 | ▶ | if ( $self->code_return_type eq 'bytes' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 14 | return $result; | |||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | 26 | $self->_encode($result); | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 41 | ▶ | sub _set_added_by { | ||||||||
|
Subroutine
_set_added_by: called CC=1 SCAR=0.0
| |||||||||||
| 103 | 41 | my ($self, $value) = @_; | |||||||||
| 104 | 41 | return $self->_push_added_by(sprintf("%s from coderef added by %s", $self->code_return_type, $value)); | |||||||||
| 105 | }; | ||||||||||
| 106 | |||||||||||
| 107 | 18 | __PACKAGE__->meta->make_immutable; | |||||||||
| 108 | 18 | 1; | |||||||||
| 109 | |||||||||||
| 110 | __END__ | ||||||||||