Venus::Role::Buildable
Buildable Role
Buildable Role for Perl 5
method: build_arg method: build_args method: build_data method: build_self method: build_nil
package Example;
use Venus::Class;
with 'Venus::Role::Buildable';
attr 'test';
package main;
my $example = Example->new;
# $example->test;
This package modifies the consuming package and provides methods for hooking into object construction of the consuming class, e.g. handling single-arg object construction.
The build_arg method, if defined, is only called during object construction when a single non-hashref is provided.
build_arg(any $data) (hashref)
{ since => '0.01', }
=example-1 build_arg
package Example1;
use Venus::Class;
attr 'x';
attr 'y';
with 'Venus::Role::Buildable';
sub build_arg {
my ($self, $data) = @_;
$data = { x => $data, y => $data };
return $data;
}
package main;
my $example = Example1->new(10);
# $example->x;
# $example->y;
The build_args method, if defined, is only called during object construction to hook into the handling of the arguments provided.
build_args(hashref $data) (hashref)
{ since => '0.01', }
=example-1 build_args
package Example2;
use Venus::Class;
attr 'x';
attr 'y';
with 'Venus::Role::Buildable';
sub build_args {
my ($self, $data) = @_;
$data->{x} ||= int($data->{x} || 100);
$data->{y} ||= int($data->{y} || 100);
return $data;
}
package main;
my $example = Example2->new(x => 10, y => 10);
# $example->x;
# $example->y;
The build_data method, if defined, is only called during object construction to hook into the handling of the arguments provided. This method is passed two hashrefs, the first containing expected arguments provided to the constructor (e.g. attributes), and the second containing all unexpected arguments. The hashref or key/value pairs returned from this method will be used in subsequent automation.
build_data(hashref $args, hashref $xargs) (hashref)
{ since => '4.15', }
=example-1 build_data
package Example5;
use Venus::Class;
attr 'x';
attr 'y';
with 'Venus::Role::Buildable';
sub build_data {
my ($self, $args, $xargs) = @_;
$args->{z} = delete $xargs->{z} if !exists $args->{z} && exists $xargs->{z};
return $args;
}
package main;
my $example = Example5->new(x => 10, y => 10, z => 10);
# $example->x;
# $example->y;
The build_self method, if defined, is only called during object construction after all arguments have been handled and set.
build_self(hashref $data) (object)
{ since => '0.01', }
=example-1 build_self
package Example3;
use Venus::Class;
attr 'x';
attr 'y';
with 'Venus::Role::Buildable';
sub build_self {
my ($self, $data) = @_;
die if !$self->x;
die if !$self->y;
return $self;
}
package main;
my $example = Example3->new(x => 10, y => 10);
# $example->x;
# $example->y;
The build_nil method, if defined, is only called during object construction when a single empty hashref is provided.
build_nil(hashref $data) (any)
{ since => '0.01', }
=example-1 build_nil
package Example4;
use Venus::Class;
attr 'x';
attr 'y';
with 'Venus::Role::Buildable';
sub build_nil {
my ($self, $data) = @_;
$data = { x => 10, y => 10 };
return $data;
}
package main;
my $example = Example4->new({});
# $example->x;
# $example->y;
t/Venus.t: present: authors t/Venus.t: present: license
22 POD Errors
The following errors were encountered while parsing the POD:
- Around line 14:
Unknown directive: =name
- Around line 22:
Unknown directive: =tagline
- Around line 30:
Unknown directive: =abstract
- Around line 38:
Unknown directive: =includes
- Around line 50:
Unknown directive: =synopsis
- Around line 77:
Unknown directive: =description
- Around line 87:
Unknown directive: =method
- Around line 93:
Unknown directive: =signature
- Around line 97:
Unknown directive: =metadata
- Around line 142:
Unknown directive: =method
- Around line 147:
Unknown directive: =signature
- Around line 151:
Unknown directive: =metadata
- Around line 197:
Unknown directive: =method
- Around line 206:
Unknown directive: =signature
- Around line 210:
Unknown directive: =metadata
- Around line 256:
Unknown directive: =method
- Around line 261:
Unknown directive: =signature
- Around line 265:
Unknown directive: =metadata
- Around line 311:
Unknown directive: =method
- Around line 316:
Unknown directive: =signature
- Around line 320:
Unknown directive: =metadata
- Around line 365:
Unknown directive: =partials