File Coverage

blib/lib/Role/HasPayload/Auto.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 20 21 95.2


line stmt bran cond sub pod time code
1             package Role::HasPayload::Auto 0.007;
2 1     1   1068 use Moose::Role;
  1         5085  
  1         4  
3             # ABSTRACT: a thing that automatically computes its payload based on attributes
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod package Example;
8             #pod use Moose;
9             #pod
10             #pod with qw(Role::HasPayload::Auto);
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 });
35             #pod
36             #pod $example->payload; # { height => 10, width => 20 }
37             #pod
38             #pod =head1 DESCRIPTION
39             #pod
40             #pod Role::HasPayload::Auto only provides one method, C<payload>, which returns a
41             #pod hashref of the name and value of every attribute on the object with the
42             #pod Role::HasPayload::Meta::Attribute::Payload trait. (The attribute value is
43             #pod gotten with the the method returned by the attribute's C<get_read_method>
44             #pod method.)
45             #pod
46             #pod This role is especially useful when combined with L<Role::HasMessage::Errf>.
47             #pod
48             #pod =cut
49              
50 1     1   6481 use Role::HasPayload::Meta::Attribute::Payload;
  1         5  
  1         118  
51              
52             sub payload {
53 1     1 0 18107 my ($self) = @_;
54              
55 1         7 my @attrs = grep { $_->does('Role::HasPayload::Meta::Attribute::Payload') }
  2         333  
56             $self->meta->get_all_attributes;
57              
58 1         82 my %payload = map {;
59 1         8 my $method = $_->get_read_method;
60 1         31 ($_->name => $self->$method)
61             } @attrs;
62              
63 1         27 return \%payload;
64             }
65              
66              
67 1     1   7 no Moose::Role;
  1         2  
  1         5  
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Role::HasPayload::Auto - a thing that automatically computes its payload based on attributes
79              
80             =head1 VERSION
81              
82             version 0.007
83              
84             =head1 SYNOPSIS
85              
86             package Example;
87             use Moose;
88              
89             with qw(Role::HasPayload::Auto);
90              
91             sub Payload { 'Role::HasPayload::Meta::Attribute::Payload' }
92              
93             has height => (
94             is => 'ro',
95             traits => [ Payload ],
96             );
97              
98             has width => (
99             is => 'ro',
100             traits => [ Payload ],
101             );
102              
103             has color => (
104             is => 'ro',
105             );
106              
107             ...then...
108              
109             my $example = Example->new({
110             height => 10,
111             width => 20,
112             color => 'blue',
113             });
114              
115             $example->payload; # { height => 10, width => 20 }
116              
117             =head1 DESCRIPTION
118              
119             Role::HasPayload::Auto only provides one method, C<payload>, which returns a
120             hashref of the name and value of every attribute on the object with the
121             Role::HasPayload::Meta::Attribute::Payload trait. (The attribute value is
122             gotten with the the method returned by the attribute's C<get_read_method>
123             method.)
124              
125             This role is especially useful when combined with L<Role::HasMessage::Errf>.
126              
127             =head1 PERL VERSION
128              
129             This library should run on perls released even a long time ago. It should work
130             on any version of perl released in the last five years.
131              
132             Although it may work on older versions of perl, no guarantee is made that the
133             minimum required version will not be increased. The version may be increased
134             for any reason, and there is no promise that patches will be accepted to lower
135             the minimum required perl.
136              
137             =head1 AUTHOR
138              
139             Ricardo Signes <cpan@semiotic.systems>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2022 by Ricardo Signes.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut