| 1 | package Articulate::Syntax; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Module::Load (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Exporter::Declare; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | 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 | 9 | ▶ | use Articulate::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use Data::DPath qw(dpath dpathr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use Hash::Merge (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use Articulate::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | use Articulate::Credentials; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use Articulate::File; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | use Articulate::Item; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 9 | ▶ | use Articulate::Location; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use Articulate::LocationSpecification; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 9 | ▶ | use Articulate::Permission; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 9 | ▶ | use Articulate::Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | use Articulate::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # sub throw_error { Articulate::Error::throw_error(@_) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # sub loc { Articulate::Location::loc(@_) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | =head3 instantiate_array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 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>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | The purpose of this function is to enable the following: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | package Articulate::SomeDelegatingComponent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | has delegates_to => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | coerce => sub{ instantiate_array(@_) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | Which means given config like the following: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | Articulate::SomeDelegatingComponent: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | delegates_to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | - My::Validation::For::Articles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | - class: My::Validation::For::Images | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | - max_width: 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | max_height: 768 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | - class: My::Validation::For::Documents | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | constructor: preset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | args: pdf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | You can be guaranteed that looping through C<< @{ $self->delegates_to } >> will always produce objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | =head3 instantiate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | Attempts to create an object from the hashref or class name provided. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 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>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 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). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | If the value is an object, the object will simply be returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | sub instantiate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 259 | ▶ | my $original = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instantiate: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 259 | ▶ | if ( blessed $original ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 155 | return $original; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | elsif ( !ref $original ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 64 | Module::Load::load($original); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 64 | ▶ | if ( $original->can('instance') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | return $original->instance() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 63 | return $original->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | elsif ( ref $original eq ref {} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 40 | my $class = $original->{class}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 40 | my $args = $original->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 40 | ▶ | if ( 1 == keys %$original and join ( '', keys %$original ) !~ /^[a-z_]/ ) { # single key that looks like a class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
1 == keys %$original and not join("", keys %$original) =~ /^[a-z_]/
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_]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 28 | $class = join '', keys %$original; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 28 | $args = $original->{$class}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 40 | ▶ | throw_error Internal => 'Instantiation failed: expecting key class, got '.(join ', ', keys %$original) unless defined $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 40 | Module::Load::load ( $class ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 40 | ▶ | my $constructor = $original->{constructor} // ($class->can('instance') ? 'instance' : 'new'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$original->{"constructor"} // ($class->can("instance") ? "instance" : "new")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 40 | ▶ | my @args = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | (defined $args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | ? (ref $args eq ref []) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ? @$args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | : $args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | : () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 40 | return $class->$constructor(@args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | sub instantiate_array { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 59 | ▶ | my $arrayref = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instantiate_array: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 59 | ▶ | return [] unless defined $arrayref; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # delegates_to => "Class::Name" should be interpreted as delegates_to => ["Class::Name"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 59 | ▶ | $arrayref = [$arrayref] unless ref $arrayref and ref $arrayref eq ref []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $arrayref and ref $arrayref eq ref []
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 []
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 59 | return [ map { instantiate $_ } @$arrayref ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =head3 from_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | sub email_address { from_meta (shift, 'schema/user/email_address'); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | This method uses Data::DPath to retrieve a field from the metadata structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | sub from_meta { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $structure = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_meta: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $item = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my @results = dpath ($item->meta)->match($structure); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | return shift @results; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | =head3 dpath_get | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | my $value = dpath_get($structure, '/path/in/structure'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | sub dpath_get { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 23 | ▶ | my $structure = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dpath_get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 23 | my $path = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 23 | my @results = dpath($path)->match($structure); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 23 | return shift @results; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =head3 dpath_set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | dpath_set($structure, '/path/in/structure', $value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | sub dpath_set { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | ▶ | my $structure = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dpath_set: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | my $path = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | my $value = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | my @results = dpathr($path)->match($structure); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | map { $$_ = $value } @results; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | return $value if @results; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | =head3 instantiate_selection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # in config: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | rules: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | alias: main_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | main_schema: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | dsn: 'csv:main.csv' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | utf8: 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | has schemata => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | coerce => sub { instantiate_selection @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | If a value other than a hash is given, returns a hash with the key 'default' and the original value instantiated. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | sub instantiate_selection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 5 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instantiate_selection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 5 | _instantiate_selection( $orig, \&instantiate ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | =head3 instantiate_array_selection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | rules: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | alias: two_rules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | two_rules: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | - Some::Rule | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | - Some::Other::Rule | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | has rules => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | coerce => sub { instantiate_array_selection @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 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>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | If a value other than a hash is given, returns a hash with the key 'default' and the original value instantiated as an array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | sub instantiate_array_selection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instantiate_array_selection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | _instantiate_selection( $orig, \&instantiate_array ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | sub _instantiate_selection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 7 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_instantiate_selection: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 7 | my $instantiate = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 7 | ▶ | if ( ref $orig eq ref {} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 5 | for my $i ( 1..5 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 25 | foreach my $this ( keys %$orig ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 180 | my $got = $orig->{$this}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 180 | ▶ | if ( is_single_key_hash( $got, 'alias' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 9 | ▶ | $orig->{$this} = $orig->{ $got->{alias} } if $i > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 171 | $orig->{$this} = $instantiate->($got); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 5 | return $orig; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | return { default => $instantiate->($orig) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | =head3 select_from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # given this config: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | schemata: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | "[Complicated] configuration: can_be->[string, hash, whatever]" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | schema_generic: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | alias: default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # if your class has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | sub schema { select_from schemata => @_ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # then you can do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | $self->schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | $self->schema('default'); # same thing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | $self->schema('schema_generic'); # same thing, because of alias | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | $self->schemata->{default}; # This is what they all do in practice | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | Implements a user-friendly selection mechanism like the one implemented by C<Dancer::Plugin::DBIC::schema>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | sub select_from { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my ($attribute, $self, $which) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_from: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | $which //= 'default'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$which //= "default"
MC/DC0%0/1
$which //= "default"
$which
Truth table0%0/2
A
A= $which
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $this = $which; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | my $selectables = $self->$attribute; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | for ( 1..5 ) { # if more than this then you probably have recusion | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my $got = $selectables->{$this}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | if ( is_single_key_hash( $got, 'alias' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | $this = $got->{alias}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | return $got; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | =head3 is_single_key_hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | is_single_key_hash ( { foo => 123 } ); # returns 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | is_single_key_hash ( { foo => 123 }, 'foo' ); # returns 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | is_single_key_hash ( { foo => 123 }, 'bar' ); # returns 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | sub is_single_key_hash { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 187 | ▶ | my $got = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_single_key_hash: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 187 | my $key = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 187 | ▶ | return 1 if ( defined $key and ref $got eq ref {} and 1 == scalar keys %$got and $key eq [keys %$got]->[0] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
defined $key and ref $got eq ref {} and 1 == scalar keys %$got and $key eq [keys %$got]->[0]
defined $key and ref $got eq ref {} and 1 == scalar keys %$got
defined $key and ref $got eq ref {}
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 177 | ▶ | return 1 if ( !defined $key and ref $got eq ref {} and 1 == scalar keys %$got ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $key and ref $got eq ref {} and 1 == scalar keys %$got
not defined $key and ref $got eq ref {}
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 176 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =head3 hash_merge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | my $merged = hash_merge ($parent, $child) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 9 | my $hash_merger = Hash::Merge->new('RIGHT_PRECEDENT'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | sub hash_merge { # very naive, will change to something like Hash::Merge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | my ($parent, $child) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash_merge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | return $hash_merger->merge($parent, $child); # todo: more | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||