Articulate/Syntax.pm

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