API/Role/WPObject.pm

filter: Statementstmt 98.0% 102 / 104 Branchbran 90.0% 18 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 95.6% 22 / 23 Podpod n/a totaltotal 92.9% 146 / 157 CC 19 SCAR 12.4
CC
19
Cov
95%
__ANON__
8.0
_deflate_datetimes
5.2
__ANON__
4.3
CRAP 3.5
↑ Summary API.pm »
1 package WP::API::Role::WPObject;
2 {
3 7 $WP::API::Role::WPObject::VERSION = '0.01';
4 }
5 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 $WP::API::Role::WPObject::AUTHORITY = 'cpan:DROLSKY';
7 }
8  
9 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use MooseX::Params::Validate qw( validated_hash );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use WP::API::Types qw( ArrayRef HashRef Maybe NonEmptyStr PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 requires '_create_result_as_params';
21  
22 7 parameter id_method => (
23 isa => NonEmptyStr,
24 required => 1,
25 );
26  
27 7 parameter xmlrpc_get_method => (
28 isa => NonEmptyStr,
29 required => 1,
30 );
31  
32 7 parameter xmlrpc_create_method => (
33 isa => NonEmptyStr,
34 required => 1,
35 );
36  
37 7 parameter fields => (
38 isa => HashRef,
39 required => 1,
40 );
41  
42 7 has api => (
43 is => 'ro',
44 isa => 'WP::API',
45 required => 1,
46 );
47  
48 7 has _raw_data => (
49 is => 'ro',
50 isa => HashRef,
51 init_arg => undef,
52 lazy => 1,
53 builder => '_build_raw_data',
54 );
55  
56 10 ▶my $_make_field_attrs = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
57 10 my $fields = shift;
58  
59 10 my @optional;
60 10 for my $field ( keys %{$fields} ) {
61 165 my $spec = $fields->{$field};
62  
63 165 my %attr_p = (
64 is => 'ro',
65 isa => $spec,
66 init_arg => undef,
67 lazy => 1,
68 );
69  
70 165 ▶ if ( $spec eq 'DateTime' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq 'DateTime') { } 25 140 165
71 25 ▶ my $datetime_method
Branch: 1 branch
BranchTrueFalseTotal
$field =~ /_gmt$/ ? : 15 10 25
72 = $field =~ /_gmt$/ ? '_gmt_datetime' : '_floating_datetime';
73 4 ▶ $attr_p{default} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 4 $_[0]->$datetime_method( $_[0]->_raw_data()->{$field} );
75 25 };
76 }
77 else {
78 140 ▶ my $default_if_missing
Branch: 2 branches
BranchTrueFalseTotal
$spec->is_a_type_of(ArrayRef()) ? : 10 130 140
$spec->is_a_type_of(HashRef()) ? : 15 115 130
79 = $spec->is_a_type_of(ArrayRef) ? []
80 : $spec->is_a_type_of(HashRef) ? {}
81 : undef;
82  
83 27 ▶ $attr_p{default} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
84 27 my $raw = $_[0]->_raw_data();
85 27 ▶ defined $raw->{$field} ? $raw->{$field} : $default_if_missing;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw->{$field} ? : 22 5 27
86 140 };
87  
88 140 ▶ if ( $spec->is_a_type_of(Maybe) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->is_a_type_of(Maybe())) 25 115 140
89 25 $attr_p{predicate} = 'has_' . $field;
90 25 push @optional, $field;
91 }
92  
93 140 ▶ if ( $spec->has_coercion() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->has_coercion) 20 120 140
94 20 $attr_p{coerce} = 1;
95 }
96 }
97  
98 165 has $field => %attr_p;
99 }
100  
101 0 ▶ method _optional_fields => sub { @optional };
Subroutine _optional_fields: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=0.4
102 7 };
103  
104 2 ▶sub _gmt_datetime {
Subroutine _gmt_datetime: called CC=1 SCAR=0.0
105 2 my $self = shift;
106 2 my $value = shift;
107  
108 2 return $self->_parse_datetime($value)->set_time_zone('UTC');
109 }
110  
111 2 ▶sub _floating_datetime {
Subroutine _floating_datetime: called CC=1 SCAR=0.0
112 2 my $self = shift;
113 2 my $value = shift;
114  
115 2 return $self->_parse_datetime($value)
116 ->set_time_zone( $self->api()->server_time_zone() );
117 }
118  
119 4 ▶sub _parse_datetime {
Subroutine _parse_datetime: called CC=1 SCAR=0.0
120 4 my $self = shift;
121 4 my $value = shift;
122  
123 4 my %parsed;
124 4 @parsed{qw(year month day hour minute second)}
125 = $value =~ /^(\d{4})(\d\d)(\d\d)T(\d\d):(\d\d):(\d\d)Z?/;
126  
127 4 return DateTime->new( %parsed, time_zone => 'floating' );
128 }
129  
130 10 ▶role {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 10 my $p = shift;
132  
133 10 $_make_field_attrs->( $p->{fields} );
134  
135 10 my $id_method = $p->id_method();
136  
137 10 has $id_method => ( is => 'ro', isa => PositiveInt, required => 1, );
138  
139 10 my $xmlrpc_get_method = $p->xmlrpc_get_method();
140  
141 2 ▶ method _build_raw_data => sub {
Subroutine _build_raw_data: called CC=4 SCAR=14.6
142 2 my $self = shift;
143  
144 2 my $raw
145 = $self->api()->call( $xmlrpc_get_method, $self->$id_method() );
146 2 for my $field ( $self->_optional_fields() ) {
147 delete $raw->{$field}
148 5 ▶ unless defined $raw->{$field} && length $raw->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw->{$field} and length $raw->{$field} 5 0 5
Condition33%1/3
defined $raw->{$field} and length $raw->{$field}
!ll&&!rl&&r
050
MC/DC0%0/2
defined $raw->{$field} and length $raw->{$field}
defined $raw->{$field} length $raw->{$field}
Truth table33%1/3
A and B
A= defined $raw->{$field}B= length $raw->{$field}
ABresult
0X0
100
111
149 }
150  
151 2 $self->_munge_raw_data($raw);
152  
153 2 return $raw;
154 10 };
155  
156 10 my $xmlrpc_create_method = $p->xmlrpc_create_method();
157  
158 2 ▶ method create => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 2 my $class = shift;
160 2 my %p = validated_hash(
161 \@_,
162 api => { isa => 'WP::API' },
163 MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
164 );
165  
166 2 my $api = delete $p{api};
167  
168 2 $class->_munge_create_parameters( \%p );
169  
170 2 my $result = $api->call(
171 $xmlrpc_create_method,
172 \%p,
173 );
174  
175 2 return $class->new(
176 $class->_create_result_as_params($result),
177 api => $api
178 );
179 10 };
180 7 };
181  
182 1 ▶sub _munge_create_parameters {
Subroutine _munge_create_parameters: called CC=1 SCAR=0.0
183 1 return;
184 }
185  
186 1 ▶sub _munge_raw_data {
Subroutine _munge_raw_data: called CC=1 SCAR=0.0
187 1 return;
188 }
189  
190 {
191 7 my $format = q{YYYMMdd'T'HH:mm:ss};
192  
193 1 ▶ sub _deflate_datetimes {
Subroutine _deflate_datetimes: called CC=5 SCAR=16.4
194 1 my $class = shift;
195 1 my $p = shift;
196 1 my @fields = @_;
197  
198 1 for my $field (@fields) {
199 4 ▶ next unless $p->{$field} && blessed $p->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{$field} and builtin::blessed($p->{$field}) 3 1 4
Condition66%2/3
$p->{$field} and builtin::blessed($p->{$field})
!ll&&!rl&&r
301
MC/DC50%1/2
$p->{$field} and builtin::blessed($p->{$field})
$p->{$field} builtin::blessed($p->{$field})
Truth table66%2/3
A and B
A= $p->{$field}B= builtin::blessed($p->{$field})
ABresult
0X0
100
111
200  
201 1 ▶ if ( $field =~ /_gmt$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /_gmt$/) { } 1 0 1
202 $p->{$field}
203 1 = $p->{$field}->clone()->set_time_zone('UTC')
204 ->format_cldr( $format );
205 }
206 else {
207 $p->{$field}
208 = $p->{$field}->clone()
209 0 ->set_time_zone( $p->{api}->server_time_zone() )
210 ->format_cldr($format);
211 }
212 }
213  
214 1 return;
215 }
216 }
217  
218 7 1;
219  
220 # ABSTRACT: A shared role for all WP::API::* objects
221  
222 __END__
↑ Summary API.pm »