1use 5.014;
2
3use strict;
4use warnings;
5
6use routines;
7
8use Test::Auto;
9use Test::More;
10
11=name
12
13Data::Object::Role::Buildable
14
15=cut
16
17=abstract
18
19Buildable Role for Perl 5
20
21=cut
22
23=synopsis
24
25  package Vehicle;
26
27  use Moo;
28
29  with 'Data::Object::Role::Buildable';
30
31  has name => (
32    is => 'rw'
33  );
34
35  1;
36
37=cut
38
39=description
40
41This package provides methods for hooking into object construction of the
42consuming class, e.g. handling single-arg object construction.
43
44=cut
45
46=scenario buildarg
47
48This package supports handling a C<build_arg> method, as a hook into object
49construction, which is called and passed a single argument if a single argument
50is passed to the constructor.
51
52=example buildarg
53
54  package Car;
55
56  use Moo;
57
58  extends 'Vehicle';
59
60  sub build_arg {
61    my ($class, $name) = @_;
62
63    # do something with $name or $class ...
64
65    return { name => $name };
66  }
67
68  package main;
69
70  my $car = Car->new('tesla');
71
72=scenario buildargs
73
74This package supports handling a C<build_args> method, as a hook into object
75construction, which is called and passed a C<hashref> during object
76construction.
77
78=example buildargs
79
80  package Sedan;
81
82  use Moo;
83
84  extends 'Car';
85
86  sub build_args {
87    my ($class, $args) = @_;
88
89    # do something with $args or $class ...
90
91    $args->{name} = ucfirst $args->{name};
92
93    return $args;
94  }
95
96  package main;
97
98  my $sedan = Sedan->new('tesla');
99
100=scenario buildself
101
102This package supports handling a C<build_self> method, as a hook into object
103construction, which is called and passed a C<hashref> during object
104construction. Note: Manipulating the arguments doesn't effect object's
105construction or properties.
106
107=example buildself
108
109  package Taxicab;
110
111  use Moo;
112
113  extends 'Sedan';
114
115  sub build_self {
116    my ($self, $args) = @_;
117
118    # do something with $self or $args ...
119
120    $args->{name} = 'Toyota';
121
122    return;
123  }
124
125  package main;
126
127  my $taxicab = Taxicab->new('tesla');
128
129=cut
130
131package main;
132
133my $test = Test::Auto->new(__FILE__);
134
135my $subtests = $test->subtests->standard;
136
137$subtests->synopsis(fun($tryable) {
138  ok my $result = $tryable->result, 'result ok';
139
140  $result;
141});
142
143$subtests->scenario('buildarg', fun($tryable) {
144  ok my $result = $tryable->result, 'result ok';
145  ok $result->isa('Car');
146  is $result->name, 'tesla';
147
148  $result;
149});
150
151$subtests->scenario('buildargs', fun($tryable) {
152  ok my $result = $tryable->result, 'result ok';
153  ok $result->isa('Sedan');
154  ok $result->isa('Car');
155  is $result->name, 'Tesla';
156
157  $result;
158});
159
160$subtests->scenario('buildself', fun($tryable) {
161  ok my $result = $tryable->result, 'result ok';
162  ok $result->isa('Taxicab');
163  ok $result->isa('Sedan');
164  ok $result->isa('Car');
165  is $result->name, 'Tesla';
166
167  $result;
168});
169
170ok 1 and done_testing;
171