Venus::Role::Coercible
Coercible Role
Coercible Role for Perl 5
method: coerce_args method: coerce_attr method: coerce_into method: coerce_onto method: coercers method: coercion
package Person;
use Venus::Class;
with 'Venus::Role::Coercible';
attr 'name';
attr 'father';
attr 'mother';
attr 'siblings';
sub coercers {
{
father => 'Person',
mother => 'Person',
name => 'Venus/String',
siblings => 'Person',
}
}
sub coerce_name {
my ($self, $code, @args) = @_;
return $self->$code(@args);
}
sub coerce_siblings {
my ($self, $code, $class, $value) = @_;
return [map $self->$code($class, $_), @$value];
}
package main;
my $person = Person->new(
name => 'me',
father => {name => 'father'},
mother => {name => 'mother'},
siblings => [{name => 'brother'}, {name => 'sister'}],
);
# $person
# bless({...}, 'Person')
# $person->name
# bless({...}, 'Venus::String')
# $person->father
# bless({...}, 'Person')
# $person->mother
# bless({...}, 'Person')
# $person->siblings
# [bless({...}, 'Person'), bless({...}, 'Person'), ...]
This package modifies the consuming package and provides methods for hooking into object construction and coercing arguments into objects and values.
The coerce_args method replaces values in the data provided with objects corresponding to the specification provided. The specification should contains key/value pairs where the keys map to class attributes (or input parameters) and the values are Venus::Space compatible package names.
coerce_args(hashref $data, hashref $spec) (hashref)
{ since => '0.07', }
=example-1 coerce_args
package main;
my $person = Person->new;
my $data = $person->coerce_args(
{
father => { name => 'father' }
},
{
father => 'Person',
},
);
# {
# father => bless({...}, 'Person'),
# }
The coerce_attr method is a surrogate accessor and gets and/or sets an instance attribute based on the coercion rules, returning the coerced value.
coerce_attr(string $name, any $value) (any)
{ since => '1.23', }
=example-1 coerce_attr
# given: synopsis
package main;
$person = Person->new(
name => 'me',
);
my $coerce_name = $person->coerce_attr('name');
# bless({value => "me"}, "Venus::String")
The coerce_into method attempts to build and return an object based on the class name and value provided, unless the value provided is already an object derived from the specified class.
coerce_into(string $class, any $value) (object)
{ since => '0.07', }
=example-1 coerce_into
package main;
my $person = Person->new;
my $friend = $person->coerce_into('Person', {
name => 'friend',
});
# bless({...}, 'Person')
The coerce_onto method attempts to build and assign an object based on the class name and value provided, as the value corresponding to the name specified, in the data provided. If the $value is omitted, the value corresponding to the name in the $data will be used.
coerce_onto(hashref $data, string $name, string $class, any $value) (object)
{ since => '0.07', }
=example-1 coerce_onto
package main;
my $person = Person->new;
my $data = { friend => { name => 'friend' } };
my $friend = $person->coerce_onto($data, 'friend', 'Person');
# bless({...}, 'Person'),
# $data was updated
#
# {
# friend => bless({...}, 'Person'),
# }
The coercers method, if defined, is called during object construction, or by the "coercion" method, and returns key/value pairs where the keys map to class attributes (or input parameters) and the values are Venus::Space compatible package names.
coercers() (hashref)
{ since => '0.02', }
=example-1 coercers
package main;
my $person = Person->new(
name => 'me',
);
my $coercers = $person->coercers;
# {
# father => "Person",
# mother => "Person",
# name => "Venus/String",
# siblings => "Person",
# }
The coercion method is called automatically during object construction but can be called manually as well, and is passed a hashref to coerce and return.
coercion(hashref $data) (hashref)
{ since => '0.02', }
=example-1 coercion
package main;
my $person = Person->new;
my $coercion = $person->coercion({
name => 'me',
});
# $coercion
# {...}
# $coercion->{name}
# bless({...}, 'Venus::String')
# $coercion->{father}
# undef
# $coercion->{mother}
# undef
# $coercion->{siblings}
# undef
t/Venus.t: present: authors t/Venus.t: present: license
28 POD Errors
The following errors were encountered while parsing the POD:
- Around line 13:
Unknown directive: =name
- Around line 21:
Unknown directive: =tagline
- Around line 29:
Unknown directive: =abstract
- Around line 37:
Unknown directive: =includes
- Around line 50:
Unknown directive: =synopsis
- Around line 143:
Unknown directive: =description
- Around line 152:
Unknown directive: =method
- Around line 159:
Unknown directive: =signature
- Around line 163:
Unknown directive: =metadata
- Around line 200:
Unknown directive: =method
- Around line 205:
Unknown directive: =signature
- Around line 209:
Unknown directive: =metadata
- Around line 254:
=cut found outside a pod block. Skipping to next block.
- Around line 265:
Unknown directive: =method
- Around line 271:
Unknown directive: =signature
- Around line 275:
Unknown directive: =metadata
- Around line 305:
Unknown directive: =method
- Around line 312:
Unknown directive: =signature
- Around line 316:
Unknown directive: =metadata
- Around line 403:
=cut found outside a pod block. Skipping to next block.
- Around line 416:
Unknown directive: =method
- Around line 423:
Unknown directive: =signature
- Around line 427:
Unknown directive: =metadata
- Around line 465:
Unknown directive: =method
- Around line 470:
Unknown directive: =signature
- Around line 474:
Unknown directive: =metadata
- Around line 556:
=cut found outside a pod block. Skipping to next block.
- Around line 588:
Unknown directive: =partials