Venus::Role::Makeable

Makeable Role

Makeable Role for Perl 5

method: make_args method: make_attr method: make_into method: make_onto method: makers method: making

package Person;

use Venus::Class 'attr', 'error', 'with';

with 'Venus::Role::Makeable';

attr 'name';
attr 'father';
attr 'mother';
attr 'siblings';

sub make {
  my ($self, $value) = @_;

  error if !ref $value;

  return $self->new($value);
}

sub makers {
  {
    father => 'Person',
    mother => 'Person',
    name => 'Venus/String',
    siblings => 'Person',
  }
}

sub make_name {
  my ($self, $code, @args) = @_;

  return $self->$code(@args);
}

sub make_siblings {
  my ($self, $code, $class, $value) = @_;

  return [map $self->$code($class, $_), @$value];
}

package main;

my $person = Person->make({
  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 using the "make" protocol, i.e. using the "make" method (which performs fatal type checking and coercions) instead of the typical "new" method.

The make_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.

make_args(HashRef $data, HashRef $spec) (HashRef)

{ since => '1.30', }

=example-1 make_args

package main;

my $person = Person->new;

my $data = $person->make_args(
  {
    father => { name => 'father' }
  },
  {
    father => 'Person',
  },
);

# {
#   father   => bless({...}, 'Person'),
# }

The make_attr method is a surrogate accessor and gets and/or sets an instance attribute based on the makers rules, returning the made value.

make_attr(Str $name, Any $value) (Any)

{ since => '1.30', }

=example-1 make_attr

# given: synopsis

package main;

$person = Person->new(
  name => 'me',
);

my $make_name = $person->make_attr('name');

# bless({value => "me"}, "Venus::String")

The make_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.

make_into(Str $class, Any $value) (Object)

{ since => '1.30', }

=example-1 make_into

package main;

my $person = Person->new;

my $friend = $person->make_into('Person', {
  name => 'friend',
});

# bless({...}, 'Person')

The make_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.

make_onto(HashRef $data, Str $name, Str $class, Any $value) (Object)

{ since => '1.30', }

=example-1 make_onto

package main;

my $person = Person->new;

my $data = { friend => { name => 'friend' } };

my $friend = $person->make_onto($data, 'friend', 'Person');

# bless({...}, 'Person'),

# $data was updated
#
# {
#   friend => bless({...}, 'Person'),
# }

The makers method, if defined, is called during object construction, or by the "making" 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.

makers() (HashRef)

{ since => '1.30', }

=example-1 makers

package main;

my $person = Person->new(
  name => 'me',
);

my $makers = $person->makers;

# {
#   father   => "Person",
#   mother   => "Person",
#   name     => "Venus/String",
#   siblings => "Person",
# }

The making method is called automatically during object construction but can be called manually as well, and is passed a hashref to make and return.

making(HashRef $data) (HashRef)

{ since => '1.30', }

=example-1 making

package main;

my $person = Person->new;

my $making = $person->making({
  name => 'me',
});

# $making
# {...}

# $making->{name}
# bless({...}, 'Venus::String')

# $making->{father}
# undef

# $making->{mother}
# undef

# $making->{siblings}
# undef

t/Venus.t: pdml: authors t/Venus.t: pdml: 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 151:

Unknown directive: =description

Around line 162:

Unknown directive: =method

Around line 169:

Unknown directive: =signature

Around line 173:

Unknown directive: =metadata

Around line 210:

Unknown directive: =method

Around line 215:

Unknown directive: =signature

Around line 219:

Unknown directive: =metadata

Around line 264:

=cut found outside a pod block. Skipping to next block.

Around line 275:

Unknown directive: =method

Around line 281:

Unknown directive: =signature

Around line 285:

Unknown directive: =metadata

Around line 315:

Unknown directive: =method

Around line 322:

Unknown directive: =signature

Around line 326:

Unknown directive: =metadata

Around line 413:

=cut found outside a pod block. Skipping to next block.

Around line 426:

Unknown directive: =method

Around line 433:

Unknown directive: =signature

Around line 437:

Unknown directive: =metadata

Around line 475:

Unknown directive: =method

Around line 480:

Unknown directive: =signature

Around line 484:

Unknown directive: =metadata

Around line 566:

=cut found outside a pod block. Skipping to next block.

Around line 598:

Unknown directive: =partials