NAME
Articulate::Syntax - Common functions and syntactic sugar for Articulate
FUNCTIONS
new_response
See Artciulate::Response.
new_request
See Artciulate::Request.
new_credentials
new_permission
new_location
See Artciulate::Location.
new_location_specification
See Artciulate::LocationSpecification.
instantiate_array
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 instantiate
.
If you pass 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 undef
, it will return an empty arrayref.
The purpose of this function is to enable the following:
package Articulate::SomeDelegatingComponent;
use Moo;
has delegates_to =>
is => 'rw',
default => sub { [] },
coerce => sub{ instantiate_array(@_) };
Which means given config like the following:
Articulate::SomeDelegatingComponent:
delegates_to:
- My::Validation::For::Articles
- class: My::Validation::For::Images
args:
- max_width: 1024
max_height: 768
- class: My::Validation::For::Documents
constructor: preset
args: pdf
You can be guaranteed that looping through @{ $self->delegates_to }
will always produce objects.
instantiate
Attempts to create an object from the hashref or class name provided.
If the value is a string, it will treat as a class name, and perform $class->new
, or, if the method exists, $class->instance
will be preferred (for instance, as provided by MooX::Singleton
).
If the value is a hashref, it will look at the values for the keys class
, constructor
, and args
. It will then attempt to perform $class->$constructor(@$args)
, unless the constructor is absent (in which case instance
or new
will be supplied), or if 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 args
is undefined).
If the value is an object, the object will simply be returned.
from_meta
sub email_address { from_meta (shift, 'schema/user/email_address'); }
This method uses Data::DPath to retrieve a field from the metadata structure.
dpath_get
my $value = dpath_get($structure, '/path/in/structure');
dpath_set
dpath_set($structure, '/path/in/structure', $value);
instantiate_selection
# in config:
rules:
default:
alias: main_schema
main_schema:
dsn: 'csv:main.csv'
utf8: 1
has schemata =>
is => 'rw',
default => sub { {} },
coerce => sub { instantiate_selection @_ };
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.
If a value other than a hash is given, returns a hash with the key 'default' and the original value instantiated.
instantiate_array_selection
rules:
default:
alias: two_rules
two_rules:
- Some::Rule
- Some::Other::Rule
has rules =>
is => 'rw',
default => sub { {} },
coerce => sub { instantiate_array_selection @_ };
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 instantiate_aray
).
If a value other than a hash is given, returns a hash with the key 'default' and the original value instantiated as an array.
select_from
# given this config:
schemata:
default:
"[Complicated] configuration: can_be->[string, hash, whatever]"
schema_generic:
alias: default
# if your class has
sub schema { select_from schemata => @_ }
# then you can do
$self->schema;
$self->schema('default'); # same thing
$self->schema('schema_generic'); # same thing, because of alias
$self->schemata->{default}; # This is what they all do in practice
Implements a user-friendly selection mechanism like the one implemented by Dancer::Plugin::DBIC::schema
.
is_single_key_hash
is_single_key_hash ( { foo => 123 } ); # returns 1
is_single_key_hash ( { foo => 123 }, 'foo' ); # returns 1
is_single_key_hash ( { foo => 123 }, 'bar' ); # returns 0
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.
hash_merge
my $merged = hash_merge ($parent, $child)
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 Hash::Merge with right precedence.