HasPayload/Auto.pm

filter: Statementstmt 100% 17 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 95.4% 21 / 22 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
payload
1.0
CRAP 1.0
« HasPayload/Merged.pm ↑ Summary HasPayload/Meta/Attribute/Payload.pm »
1 package Role::HasPayload::Auto 0.007;
2 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶use Role::HasPayload::Meta::Attribute::Payload;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 ▶sub payload {
Subroutine payload: called CC=1 SCAR=0.0
Pod undocumented
53 1 my ($self) = @_;
54  
55 1 my @attrs = grep { $_->does('Role::HasPayload::Meta::Attribute::Payload') }
56 $self->meta->get_all_attributes;
57  
58 1 my %payload = map {;
59 1 my $method = $_->get_read_method;
60 1 ($_->name => $self->$method)
61 } @attrs;
62  
63 1 return \%payload;
64 }
65  
66  
67 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 1;
69  
70 __END__
« HasPayload/Merged.pm ↑ Summary HasPayload/Meta/Attribute/Payload.pm »