File Coverage

blib/lib/Role/HasPayload/Merged.pm
Criterion Covered Total %
statement 22 22 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 28 29 96.5


line stmt bran cond sub pod time code
1             package Role::HasPayload::Merged 0.007;
2 1     1   911 use Moose::Role;
  1         4132  
  1         3  
3             # ABSTRACT: merge autogenerated payload with constructor-specified payload
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod package Example;
8             #pod use Moose;
9             #pod
10             #pod with qw(Role::HasPayload::Merged);
11             #pod
12             #pod sub Payload { 'Role::HasPayload::Meta::Attribute::Payload' }
13             #pod
14             #pod has height => (
15             #pod is => 'ro',
16             #pod traits => [ Payload ],
17             #pod );
18             #pod
19             #pod has width => (
20             #pod is => 'ro',
21             #pod traits => [ Payload ],
22             #pod );
23             #pod
24             #pod has color => (
25             #pod is => 'ro',
26             #pod );
27             #pod
28             #pod ...then...
29             #pod
30             #pod my $example = Example->new({
31             #pod height => 10,
32             #pod width => 20,
33             #pod color => 'blue',
34             #pod payload => { depth => 30 },
35             #pod });
36             #pod
37             #pod $example->payload; # { height => 10, width => 20, depth => 30 }
38             #pod
39             #pod =head1 DESCRIPTION
40             #pod
41             #pod Role::HasPayload::Merged provides a C<payload> method and a C<payload>
42             #pod attribute. It computes the result of the C<payload> method when it's called,
43             #pod first by gathering the values of attributes marked with
44             #pod Role::HasPayload::Meta::Attribute::Payload, then by merging in the contents of
45             #pod the C<payload> attribute (provided at construction).
46             #pod
47             #pod If an entry in the constructor-provided payload already exists in the
48             #pod autogenerated payload, a warning is issued and the autogenerated value is used.
49             #pod
50             #pod For a bit more on the autogenerated payload, see L<Role::HasPayload::Auto>.
51             #pod
52             #pod This role is especially useful when combined with L<Role::HasMessage::Errf>.
53             #pod
54             #pod =cut
55              
56 1     1   5208 use Role::HasPayload::Meta::Attribute::Payload;
  1         3  
  1         187  
57              
58             has payload => (
59             reader => '_payload_to_merge',
60             isa => 'HashRef',
61             default => sub { {} },
62             );
63              
64             sub payload {
65 2     2 0 15468 my ($self) = @_;
66              
67 2         8 my @attrs = grep { $_->does('Role::HasPayload::Meta::Attribute::Payload') }
  6         514  
68             $self->meta->get_all_attributes;
69              
70 2         245 my %payload = map {;
71 2         11 my $method = $_->get_read_method;
72 2         43 ($_->name => $self->$method)
73             } @attrs;
74              
75 2         84 my $manual_payload = $self->_payload_to_merge;
76 2         6 KEY: for my $key (keys %$manual_payload) {
77 3 100       10 if (exists $payload{ $key }) {
78 1         155 Carp::carp("declining to override automatic payload entry $key");
79 1         51 next KEY;
80             }
81              
82 2         5 $payload{ $key } = $manual_payload->{ $key };
83             }
84              
85 2         14 return \%payload;
86             }
87              
88 1     1   6 no Moose::Role;
  1         2  
  1         6  
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             Role::HasPayload::Merged - merge autogenerated payload with constructor-specified payload
100              
101             =head1 VERSION
102              
103             version 0.007
104              
105             =head1 SYNOPSIS
106              
107             package Example;
108             use Moose;
109              
110             with qw(Role::HasPayload::Merged);
111              
112             sub Payload { 'Role::HasPayload::Meta::Attribute::Payload' }
113              
114             has height => (
115             is => 'ro',
116             traits => [ Payload ],
117             );
118              
119             has width => (
120             is => 'ro',
121             traits => [ Payload ],
122             );
123              
124             has color => (
125             is => 'ro',
126             );
127              
128             ...then...
129              
130             my $example = Example->new({
131             height => 10,
132             width => 20,
133             color => 'blue',
134             payload => { depth => 30 },
135             });
136              
137             $example->payload; # { height => 10, width => 20, depth => 30 }
138              
139             =head1 DESCRIPTION
140              
141             Role::HasPayload::Merged provides a C<payload> method and a C<payload>
142             attribute. It computes the result of the C<payload> method when it's called,
143             first by gathering the values of attributes marked with
144             Role::HasPayload::Meta::Attribute::Payload, then by merging in the contents of
145             the C<payload> attribute (provided at construction).
146              
147             If an entry in the constructor-provided payload already exists in the
148             autogenerated payload, a warning is issued and the autogenerated value is used.
149              
150             For a bit more on the autogenerated payload, see L<Role::HasPayload::Auto>.
151              
152             This role is especially useful when combined with L<Role::HasMessage::Errf>.
153              
154             =head1 PERL VERSION
155              
156             This library should run on perls released even a long time ago. It should work
157             on any version of perl released in the last five years.
158              
159             Although it may work on older versions of perl, no guarantee is made that the
160             minimum required version will not be increased. The version may be increased
161             for any reason, and there is no promise that patches will be accepted to lower
162             the minimum required perl.
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