Articulate/Syntax.pm

filter: Statementstmt 86.9% 107 / 123 Branchbran 83.3% 30 / 36 Conditioncond 88.4% 23 / 26 MC/DCmcdc 78.5% 11 / 14 Subroutinesub 89.2% 25 / 28 Podpod 100% 10 / 10 totaltotal 86.9% 206 / 237 CC 31 SCAR 18.5
CC
31
Cov
86%
select_from
20.0
instantiate
12.1
is_single_key_hash
8.0
CRAP 6.4
« Articulate/Location.pm ↑ Summary Articulate/Service.pm »
1 package Articulate::Syntax;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Exporter::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 default_exports qw(
10 instantiate instantiate_array instantiate_selection instantiate_array_selection
11 response articulate_request
12 credentials permission
13 loc locspec
14 dpath_get dpath_set
15 hash_merge
16 throw_error
17 select_from
18 is_single_key_hash
19 );
20  
21 10 ▶use Articulate::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use Data::DPath qw(dpath dpathr);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 ▶use Articulate::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use Articulate::Credentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use Articulate::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 ▶use Articulate::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 10 ▶use Articulate::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 10 ▶use Articulate::LocationSpecification;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 10 ▶use Articulate::Permission;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 10 ▶use Articulate::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 10 ▶use Articulate::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # sub throw_error { Articulate::Error::throw_error(@_) };
36 # sub loc { Articulate::Location::loc(@_) };
37  
38 =head3 instantiate_array
39  
40 C<instantiate_array> accepts an arrayref of values which represent objects. For each value, if it is not an object, it will attempt to instantiate one using C<instantiate>.
41  
42 If you pass C<instantiate_array> a value which is not an arrayref, it will assume you meant to give it an arrayref with a single item; or, if you pass it C<undef>, it will return an empty arrayref.
43  
44 The purpose of this function is to enable the following:
45  
46 package Articulate::SomeDelegatingComponent;
47 use Moo;
48 has delegates_to =>
49 is => 'rw',
50 default => sub { [] },
51 coerce => sub{ instantiate_array(@_) };
52  
53 Which means given config like the following:
54  
55 Articulate::SomeDelegatingComponent:
56 delegates_to:
57 - My::Validation::For::Articles
58 - class: My::Validation::For::Images
59 args:
60 - max_width: 1024
61 max_height: 768
62 - class: My::Validation::For::Documents
63 constructor: preset
64 args: pdf
65  
66 You can be guaranteed that looping through C<< @{ $self->delegates_to } >> will always produce objects.
67  
68 =head3 instantiate
69  
70 Attempts to create an object from the hashref or class name provided.
71  
72 If the value is a string, it will treat as a class name, and perform C<< $class->new >>, or, if the method exists, C<< $class->instance >> will be preferred (for instance, as provided by C<MooX::Singleton>).
73  
74 If the value is a hashref, it will look at the values for the keys C<class>, C<constructor>, and C<args>. It will then attempt to perform C<< $class->$constructor(@$args) >>, unless the constructor is absent (in which case C<instance> or C<new> will be supplied), or if C<args> is not an arrayref, in which case it will be passed to the constructor as a single argument (or the empty list will be passed if C<args> is undefined).
75  
76 If the value is an object, the object will simply be returned.
77  
78 =cut
79  
80 sub instantiate {
81 352 ▶ my $original = shift;
Subroutine instantiate: called CC=12 SCAR=24.9
Pod documented
82 352 ▶ if ( blessed $original ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($original)) { } 212 140 352
elsif (not ref $original) { } 86 54 140
elsif (ref $original eq ref {}) { } 54 0 54
83 212 return $original;
84 }
85 elsif ( !ref $original ) {
86 86 Module::Load::load($original);
87 86 ▶ if ( $original->can('instance') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($original->can('instance')) { } 1 85 86
88 1 return $original->instance();
89 }
90 else {
91 85 return $original->new();
92 }
93 }
94 elsif ( ref $original eq ref {} ) {
95 54 my $class = $original->{class};
96 54 my $args = $original->{args};
97 54 ▶ if ( 1 == keys %$original and join( '', keys %$original ) !~ /^[a-z_]/ )
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %$original and not join("", keys %$original) =~ /^[a-z_]/) 41 13 54
Condition100%3/3
1 == keys %$original and not join("", keys %$original) =~ /^[a-z_]/
!ll&&!rl&&r
12141
MC/DC100%2/2
1 == keys %$original and not join("", keys %$original) =~ /^[a-z_]/
1 == keys %$original not join("", keys %$original) =~ /^[a-z_]/
Truth table100%3/3
A and not(B)
A= 1 == keys %$originalB= not join("", keys %$original) =~ /^[a-z_]/
ABresult
0X0
100
111
98 { # single key that looks like a class
99 41 $class = join '', keys %$original;
100 41 $args = $original->{$class};
101 }
102 54 ▶ throw_error Internal => 'Instantiation failed: expecting key class, got '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 54 54
103 . ( join ', ', keys %$original )
104 unless defined $class;
105 54 Module::Load::load($class);
106 my $constructor = $original->{constructor}
107 54 ▶ // ( $class->can('instance') ? 'instance' : 'new' );
Branch: 1 branch
BranchTrueFalseTotal
$class->can('instance') ? : 5 47 52
Condition66%2/3
$original->{"constructor"} // ($class->can("instance") ? "instance" : "new")
l!l&&r!l&&!r
2520
MC/DC0%0/2
$original->{"constructor"} // ($class->can("instance") ? "instance" : "new")
$original->{"constructor"} ($class->can("instance") ? "instance" : "new")
Truth table66%2/3
A // B
A= $original->{"constructor"}B= ($class->can("instance") ? "instance" : "new")
ABresult
1X1
011
000
108 54 ▶ my @args = (
Branch: 2 branches
BranchTrueFalseTotal
defined $args ? : 53 1 54
ref $args eq ref [] ? : 6 47 53
109 ( defined $args )
110 ? ( ref $args eq ref [] )
111 ? @$args
112 : $args
113 : ()
114 );
115 54 return $class->$constructor(@args);
116 }
117 }
118  
119 sub instantiate_array {
120 74 ▶ my $arrayref = shift;
Subroutine instantiate_array: called CC=4 SCAR=13.9
Pod documented
121 74 ▶ return [] unless defined $arrayref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arrayref 0 74 74
122  
123 # delegates_to => "Class::Name" should be interpreted as delegates_to => ["Class::Name"]
124 74 ▶ $arrayref = [$arrayref] unless ref $arrayref and ref $arrayref eq ref [];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayref and ref $arrayref eq ref [] 19 55 74
Condition100%3/3
ref $arrayref and ref $arrayref eq ref []
!ll&&!rl&&r
51455
MC/DC100%2/2
ref $arrayref and ref $arrayref eq ref []
ref $arrayref ref $arrayref eq ref []
Truth table100%3/3
A and B
A= ref $arrayrefB= ref $arrayref eq ref []
ABresult
0X0
100
111
125 74 return [ map { instantiate $_ } @$arrayref ];
126 }
127  
128 =head3 from_meta
129  
130 sub email_address { from_meta (shift, 'schema/user/email_address'); }
131  
132 This method uses Data::DPath to retrieve a field from the metadata structure.
133  
134 =cut
135  
136 sub from_meta {
137 0 ▶ my $structure = shift;
Subroutine from_meta: not called CC=1 SCAR=6.9
Pod documented
138 0 my $item = shift;
139 0 my @results = dpath( $item->meta )->match($structure);
140 0 return shift @results;
141 }
142  
143 =head3 dpath_get
144  
145 my $value = dpath_get($structure, '/path/in/structure');
146  
147 =cut
148  
149 sub dpath_get {
150 23 ▶ my $structure = shift;
Subroutine dpath_get: called CC=1 SCAR=0.0
Pod documented
151 23 my $path = shift;
152 23 my @results = dpath($path)->match($structure);
153 23 return shift @results;
154 }
155  
156 =head3 dpath_set
157  
158 dpath_set($structure, '/path/in/structure', $value);
159  
160 =cut
161  
162 sub dpath_set {
163 1 ▶ my $structure = shift;
Subroutine dpath_set: called CC=2 SCAR=7.0
Pod documented
164 1 my $path = shift;
165 1 my $value = shift;
166 1 my @results = dpathr($path)->match($structure);
167 1 map { $$_ = $value } @results;
168 1 ▶ return $value if @results;
Branch: 1 branch
BranchTrueFalseTotal
if @results 1 0 1
169 }
170  
171 =head3 instantiate_selection
172  
173 # in config:
174  
175 rules:
176 default:
177 alias: main_schema
178 main_schema:
179 dsn: 'csv:main.csv'
180 utf8: 1
181  
182 has schemata =>
183 is => 'rw',
184 default => sub { {} },
185 coerce => sub { instantiate_selection @_ };
186  
187 Expects a hash. If any of the values are single-key hashes with the key 'alias' then the alias is resolved. Otherwise the value is instantiated.
188  
189 If a value other than a hash is given, returns a hash with the key 'default' and the original value instantiated.
190  
191 =cut
192  
193 sub instantiate_selection {
194 6 ▶ my $orig = shift;
Subroutine instantiate_selection: called CC=1 SCAR=0.0
Pod documented
195 6 _instantiate_selection( $orig, \&instantiate );
196 }
197  
198 =head3 instantiate_array_selection
199  
200 rules:
201 default:
202 alias: two_rules
203 two_rules:
204 - Some::Rule
205 - Some::Other::Rule
206  
207 has rules =>
208 is => 'rw',
209 default => sub { {} },
210 coerce => sub { instantiate_array_selection @_ };
211  
212 Expects a hash. If any of the values are single-key hashes with the key 'alias' then the alias is resolved. Otherwise the value is instantiated as an array (see C<instantiate_aray>).
213  
214 If a value other than a hash is given, returns a hash with the key 'default' and the original value instantiated as an array.
215  
216 =cut
217  
218 sub instantiate_array_selection {
219 2 ▶ my $orig = shift;
Subroutine instantiate_array_selection: called CC=1 SCAR=0.0
Pod documented
220 2 _instantiate_selection( $orig, \&instantiate_array );
221 }
222  
223 sub _instantiate_selection {
224 8 ▶ my $orig = shift;
Subroutine _instantiate_selection: called CC=6 SCAR=17.9
225 8 my $instantiate = shift;
226 8 ▶ if ( ref $orig eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $orig eq ref {}) { } 6 2 8
227 6 for my $i ( 1 .. 5 ) {
228 30 foreach my $this ( keys %$orig ) {
229 250 my $got = $orig->{$this};
230 250 ▶ if ( is_single_key_hash( $got, 'alias' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_single_key_hash($got, 'alias')) { } 8 242 250
231 8 ▶ $orig->{$this} = $orig->{ $got->{alias} } if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 4 4 8
232 }
233 else {
234 242 $orig->{$this} = $instantiate->($got);
235 }
236 }
237 }
238 6 return $orig;
239 }
240 else {
241 2 return { default => $instantiate->($orig) };
242 }
243 }
244  
245 =head3 select_from
246  
247 # given this config:
248  
249 schemata:
250 default:
251 "[Complicated] configuration: can_be->[string, hash, whatever]"
252 schema_generic:
253 alias: default
254  
255 # if your class has
256 sub schema { select_from schemata => @_ }
257  
258 # then you can do
259 $self->schema;
260 $self->schema('default'); # same thing
261 $self->schema('schema_generic'); # same thing, because of alias
262 $self->schemata->{default}; # This is what they all do in practice
263  
264 Implements a user-friendly selection mechanism like the one implemented by C<Dancer::Plugin::DBIC::schema>.
265  
266 =cut
267  
268 sub select_from {
269 0 ▶ my ( $attribute, $self, $which ) = @_;
Subroutine select_from: not called CC=4 SCAR=30.0
Pod documented
270 0 ▶ $which //= 'default';
Condition0%0/2
$which //= "default"
l!l
00
MC/DC0%0/1
$which //= "default"
$which
Truth table0%0/2
A
A= $which
Aresult
11
00
271 0 my $this = $which;
272 0 my $selectables = $self->$attribute;
273 0 for ( 1 .. 5 ) { # if more than this then you probably have recusion
274 0 my $got = $selectables->{$this};
275 0 ▶ if ( is_single_key_hash( $got, 'alias' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_single_key_hash($got, "alias")) 0 0 0
276 0 $this = $got->{alias};
277 0 next;
278 }
279 0 return $got;
280 }
281 }
282  
283 =head3 is_single_key_hash
284  
285 is_single_key_hash ( { foo => 123 } ); # returns 1
286 is_single_key_hash ( { foo => 123 }, 'foo' ); # returns 1
287 is_single_key_hash ( { foo => 123 }, 'bar' ); # returns 0
288  
289 Returns 1 if the first argument is a hashref with exactly one key. If a second argument is provided, then the key, if it exists, must be equal to that argument, or the return value will be 0.
290  
291 =cut
292  
293 sub is_single_key_hash {
294 257 ▶ my $got = shift;
Subroutine is_single_key_hash: called CC=8 SCAR=20.8
Pod documented
295 257 my $key = shift;
296 257 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and ref $got eq ref {} and 1 == scalar keys %$got and $key eq [keys %$got]->[0] 9 248 257
Condition100%9/9
defined $key and ref $got eq ref {} and 1 == scalar keys %$got and $key eq [keys %$got]->[0]
!ll&&!rl&&r
207419
defined $key and ref $got eq ref {} and 1 == scalar keys %$got
!ll&&!rl&&r
206150
defined $key and ref $got eq ref {}
!ll&&!rl&&r
420251
MC/DC100%4/4
defined $key and ref $got eq ref {} and 1 == scalar keys %$got and $key eq [keys %$got]->[0]
defined $key ref $got eq ref {} 1 == scalar keys %$got $key eq [keys %$got]->[0]
Truth table100%5/5
A and B and C and D
A= defined $keyB= ref $got eq ref {}C= 1 == scalar keys %$gotD= $key eq [keys %$got]->[0]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
297 if (defined $key
298 and ref $got eq ref {}
299 and 1 == scalar keys %$got
300 and $key eq [ keys %$got ]->[0] );
301 248 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key and ref $got eq ref {} and 1 == scalar keys %$got 1 247 248
Condition100%6/6
not defined $key and ref $got eq ref {} and 1 == scalar keys %$got
!ll&&!rl&&r
24611
not defined $key and ref $got eq ref {}
!ll&&!rl&&r
24422
MC/DC100%3/3
not defined $key and ref $got eq ref {} and 1 == scalar keys %$got
not defined $key ref $got eq ref {} 1 == scalar keys %$got
Truth table100%4/4
not(A) and B and C
A= not defined $keyB= ref $got eq ref {}C= 1 == scalar keys %$got
ABCresult
0XX0
10X0
1100
1111
302 if ( !defined $key and ref $got eq ref {} and 1 == scalar keys %$got );
303 247 return 0;
304 }
305  
306 =head3 hash_merge
307  
308 my $merged = hash_merge ($parent, $child)
309  
310 Returns a new hashref whose values represent a union of the parent's and the child's. The child's values overwrite the parent, in case of conflict. The merge is deep (i.e. it handles nested hashes), using L<Hash::Merge> with right precedence.
311  
312 =cut
313  
314 10 my $hash_merger = Hash::Merge->new('RIGHT_PRECEDENT');
315  
316 sub hash_merge { # very naive, will change to something like Hash::Merge
317 0 ▶ my ( $parent, $child ) = @_;
Subroutine hash_merge: not called CC=1 SCAR=6.9
Pod documented
318 0 return $hash_merger->merge( $parent, $child ); # todo: more
319 }
320  
321 10 1;
« Articulate/Location.pm ↑ Summary Articulate/Service.pm »