|
NAMEVenus::Role::Coercible - Coercible Role ABSTRACTCoercible Role for Perl 5 SYNOPSIS 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'), ...]
DESCRIPTIONThis package modifies the consuming package and provides methods for hooking into object construction and coercing arguments into objects and values. METHODSThis package provides the following methods: coerce_argscoerce_args(hashref $data, hashref $spec) (hashref) 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. Since 0.07
coerce_attrcoerce_attr(string $name, any $value) (any) 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. Since 1.23
coerce_intocoerce_into(string $class, any $value) (object) 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. Since 0.07
coerce_ontocoerce_onto(hashref $data, string $name, string $class, any $value) (object) 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. Since 0.07
coercerscoercers() (hashref) 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. Since 0.02
coercioncoercion(hashref $data) (hashref) 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. Since 0.02
AUTHORSAwncorp, "awncorp@cpan.org" LICENSECopyright (C) 2022, Awncorp, "awncorp@cpan.org". This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.
|