Method/Generate/Accessor/Role/Monjon.pm

filter: Statementstmt 91.5% 98 / 107 Branchbran 91.6% 11 / 12 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 83.3% 20 / 24 Podpod 0.0% 0 / 2 totaltotal 81.2% 134 / 165 CC 13 SCAR 9.6
CC
13
Cov
87%
BEGIN
5.4
target_class
5.0
_calculate_offset
4.0
CRAP 2.6
« Method/Generate/Constructor/Role/Monjon.pm ↑ Summary Monjon.pm »
1 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Sub::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Method::Generate::Accessor::Role::Monjon;
9  
10 2 our $AUTHORITY = 'cpan:TOBYINK';
11 2 our $VERSION = '0.004';
12  
13 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 my $_fh;
16 2 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=16.9
17 0 ▶ $_fh ||= eval { require Hash::FieldHash; \&Hash::FieldHash::fieldhashes };
Condition33%1/3
$_fh ||= eval { do { require Hash::FieldHash; \&Hash::FieldHash::fieldhashes } }
l!l&&r!l&&!r
002
MC/DC0%0/2
$_fh ||= eval { do { require Hash::FieldHash; \&Hash::FieldHash::fieldhashes } }
$_fh eval { do { require Hash::FieldHash; \&Hash::FieldHash::fieldhashes } }
Truth table33%1/3
A ||= B
A= $_fhB= eval { do { require Hash::FieldHash; \&Hash::FieldHash::fieldhashes } }
ABresult
1X1
011
000
18 0 ▶ $_fh ||= eval { require Hash::Util::FieldHash::Compat; \&Hash::Util::FieldHash::Compat::fieldhash };
Condition33%1/3
$_fh ||= eval { do { require Hash::Util::FieldHash::Compat; \&Hash::Util::FieldHash::Compat::fieldhash } }
l!l&&r!l&&!r
002
MC/DC0%0/2
$_fh ||= eval { do { require Hash::Util::FieldHash::Compat; \&Hash::Util::FieldHash::Compat::fieldhash } }
$_fh eval { do { require Hash::Util::FieldHash::Compat; \&Hash::Util::FieldHash::Compat::fieldhash } }
Truth table33%1/3
A ||= B
A= $_fhB= eval { do { require Hash::Util::FieldHash::Compat; \&Hash::Util::FieldHash::Compat::fieldhash } }
ABresult
1X1
011
000
19 2 ▶ $_fh ||= do { require Hash::Util::FieldHash; \&Hash::Util::FieldHash::fieldhash };
Condition33%1/3
$_fh ||= do { require Hash::Util::FieldHash; \&Hash::Util::FieldHash::fieldhash }
l!l&&r!l&&!r
020
MC/DC0%0/2
$_fh ||= do { require Hash::Util::FieldHash; \&Hash::Util::FieldHash::fieldhash }
$_fh do { require Hash::Util::FieldHash; \&Hash::Util::FieldHash::fieldhash }
Truth table33%1/3
A ||= B
A= $_fhB= do { require Hash::Util::FieldHash; \&Hash::Util::FieldHash::fieldhash }
ABresult
1X1
011
000
20 };
21  
22 2 $_fh->(\(our %FIELDS));
23 2 $_fh->(\(my %INV_MAKERS));
24  
25 sub target_class
26 20 ▶{
Subroutine target_class: called CC=5 SCAR=16.1
Pod undocumented
27 20 my $self = shift;
28 20 ▶ if (not defined $INV_MAKERS{$self})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $INV_MAKERS{$self}) 3 17 20
29 {
30 3 for my $class (keys %Moo::MAKERS)
31 {
32 10 ▶ next unless defined $Moo::MAKERS{$class}{accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Moo::MAKERS{$class}{"accessor"} 0 10 10
33 10 ▶ next unless $self == $Moo::MAKERS{$class}{accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless $self == $Moo::MAKERS{$class}{"accessor"} 7 3 10
34 3 return( $INV_MAKERS{$self} = $class );
35 }
36 }
37 17 return $INV_MAKERS{$self};
38 }
39  
40 2 my $order = 0;
41 sub _monjon_canonicalize
42 10 ▶{
Subroutine _monjon_canonicalize: called CC=1 SCAR=0.0
43 10 my $self = shift;
44 10 my ($me, $name, $spec) = @_;
45
46 10 $spec->{_order} =++ $order;
47
48 # TODO
49 }
50  
51 around generate_method => sub
52 10 ▶{
Subroutine __ANON__: called CC=1 SCAR=0.0
53 10 my $next = shift;
54 10 my $self = shift;
55 10 $self->_monjon_canonicalize(@_);
56 2 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 10 local $Method::Generate::Accessor::CAN_HAZ_XS = 0;
58 10 $self->$next(@_);
59 2 };
60  
61 2 my $P = __PACKAGE__ . "::";
62  
63 sub _generate_simple_has
64 1 ▶{
Subroutine _generate_simple_has: called CC=1 SCAR=0.0
65 1 my $self = shift;
66 1 my ($me, $name, $spec) = @_;
67 1 my $name_str = quotemeta($name);
68 1 "exists(\$${P}FIELDS{${me}}{\"${name_str}\"})";
69 }
70  
71 sub _generate_simple_clear
72 0 ▶{
Subroutine _generate_simple_clear: not called CC=1 SCAR=6.9
73 0 my $self = shift;
74 0 my ($me, $name, $spec) = @_;
75 0 my $name_str = quotemeta($name);
76 0 "delete(\$${P}FIELDS{${me}}{\"${name_str}\"})";
77 }
78  
79 sub _generate_simple_get
80 1 ▶{
Subroutine _generate_simple_get: called CC=1 SCAR=0.0
81 1 my $self = shift;
82 1 my ($me, $name, $spec) = @_;
83 1 my $name_str = quotemeta($name);
84 1 "\$${P}FIELDS{${me}}{\"${name_str}\"}";
85 }
86  
87 sub _generate_core_set
88 2 ▶{
Subroutine _generate_core_set: called CC=1 SCAR=0.0
89 2 my $self = shift;
90 2 my ($me, $name, $spec, $value) = @_;
91 2 my $name_str = quotemeta($name);
92 2 "\$${P}FIELDS{${me}}{\"${name_str}\"} = ${value}";
93 }
94  
95 sub _generate_xs
96 0 ▶{
Subroutine _generate_xs: not called CC=1 SCAR=6.9
97 0 die "Can't generate XS accessors for Monjon accessors";
98 }
99  
100 sub default_construction_string
101 2 ▶{
Subroutine default_construction_string: called CC=1 SCAR=0.0
Pod undocumented
102 2 my $self = shift;
103
104 2 my $ctor = 'Monjon'->_constructor_maker_for($self->target_class);
105 2 my $all = $ctor->all_attribute_specs;
106 my $total = List::Util::sum(
107 0,
108 map {
109 2 $self->_calculate_length(undef, $_, $all->{$_})
110 } $ctor->monjon_fields,
111 );
112
113 2 sprintf(
114 '\(my $s = "\0" x %d)',
115 $total,
116 );
117 }
118  
119 2 my @generators = qw(
120 _generate_simple_has
121 _generate_simple_clear
122 _generate_simple_get
123 _generate_core_set
124 );
125 2 for my $method (@generators)
126 {
127 8 my $packed_generator = "$method\_packed";
128 22 ▶ around $method => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
129 22 my $next = shift;
130 22 my $self = shift;
131 22 my ($me, $name, $spec) = @_;
132 exists($spec->{pack})
133 22 ▶ ? $self->$packed_generator(@_)
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'pack'} ? : 18 4 22
134 : $self->$next(@_);
135 8 };
136 }
137  
138 sub _generate_simple_has_packed
139 0 ▶{
Subroutine _generate_simple_has_packed: not called CC=1 SCAR=6.9
140 0 return '(1)';
141 }
142  
143 sub _generate_simple_clear_packed
144 0 ▶{
Subroutine _generate_simple_clear_packed: not called CC=1 SCAR=6.9
145 0 die "This attribute cannot have a clearer; bailing out";
146 }
147  
148 sub _generate_simple_get_packed
149 11 ▶{
Subroutine _generate_simple_get_packed: called CC=1 SCAR=0.0
150 11 my $self = shift;
151 11 my ($me, $name, $spec) = @_;
152 sprintf(
153 'unpack(q(%s), substr(${%s}, %d, %d))',
154 $spec->{pack},
155 11 $me,
156 $self->_calculate_offset(@_),
157 $self->_calculate_length(@_),
158 );
159 }
160  
161 sub _generate_core_set_packed
162 7 ▶{
Subroutine _generate_core_set_packed: called CC=1 SCAR=0.0
163 7 my $self = shift;
164 7 my ($me, $name, $spec, $value) = @_;
165 sprintf(
166 'substr(${%s}, %d, %d) = pack(q(%s), %s)',
167 $me,
168 $self->_calculate_offset(@_),
169 $self->_calculate_length(@_),
170 $spec->{pack},
171 7 $value,
172 );
173 }
174  
175 sub _calculate_offset
176 18 ▶{
Subroutine _calculate_offset: called CC=4 SCAR=13.9
177 18 my $self = shift;
178 18 my ($me, $name, $spec) = @_;
179 18 my $target = $self->target_class;
180
181 18 ▶ if (not defined $spec->{_pack_offset}{$target})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $spec->{"_pack_offset"}{$target}) 5 13 18
182 {
183 5 my $ctor = 'Monjon'->_constructor_maker_for($target);
184 5 my $all = $ctor->all_attribute_specs;
185
186 5 my $offset = 0;
187 5 for my $field ( $ctor->monjon_fields )
188 {
189 7 ▶ last if $field eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $field eq $name 5 2 7
190 2 $offset += $self->_calculate_length($me, $field, $all->{$field});
191 }
192 5 $spec->{_pack_offset}{$target} = $offset;
193 }
194
195 18 $spec->{_pack_offset}{$target};
196 }
197  
198 sub _calculate_length
199 23 ▶{
Subroutine _calculate_length: called CC=2 SCAR=7.0
200 23 my $self = shift;
201 23 my ($me, $name, $spec) = @_;
202 23 ▶ $spec->{_pack_length} ||= length pack($spec->{pack}, 0);
Condition66%2/3
$spec->{"_pack_length"} ||= length pack($spec->{"pack"}, 0)
l!l&&r!l&&!r
1940
MC/DC0%0/2
$spec->{"_pack_length"} ||= length pack($spec->{"pack"}, 0)
$spec->{"_pack_length"} length pack($spec->{"pack"}, 0)
Truth table66%2/3
A ||= B
A= $spec->{"_pack_length"}B= length pack($spec->{"pack"}, 0)
ABresult
1X1
011
000
203 }
204  
205 2 1;
« Method/Generate/Constructor/Role/Monjon.pm ↑ Summary Monjon.pm »