File Coverage

blib/lib/Data/Object/Role/Buildable.pm
Criterion Covered Total %
statement 5 6 83.3
branch n/a
condition n/a
subroutine 2 3 66.6
pod 0 1 0.0
total 7 10 70.0


line stmt bran cond sub pod time code
1             package Data::Object::Role::Buildable;
2              
3 1     1   31447 use 5.014;
  1         3  
4              
5 1     1   501 use Moo::Role;
  1         9557  
  1         6  
6              
7             our $VERSION = '0.03'; # VERSION
8              
9             sub BUILD {
10              
11 0     0 0   return $_[0];
12             }
13              
14             around BUILD => sub {
15             my ($orig, $self, $args) = @_;
16              
17             if ($self->can('build_self')) {
18             $self->build_self($args);
19             }
20              
21             return $self;
22             };
23              
24             around BUILDARGS => sub {
25             my ($orig, $class, @args) = @_;
26              
27             # build_arg accepts a single-arg (non-hash)
28             my $inflate = @args == 1 && ref $args[0] ne 'HASH';
29              
30             # single argument
31             if ($class->can('build_arg') && $inflate) {
32             @args = ($class->build_arg($args[0]));
33             }
34              
35             # build_args should not accept a single-arg (non-hash)
36             my $ignore = @args == 1 && ref $args[0] ne 'HASH';
37              
38             # standard arguments
39             if ($class->can('build_args') && !$ignore) {
40             @args = ($class->build_args(@args == 1 ? $args[0] : {@args}));
41             }
42              
43             return $class->$orig(@args);
44             };
45              
46             1;
47              
48             =encoding utf8
49              
50             =head1 NAME
51              
52             Data::Object::Role::Buildable
53              
54             =cut
55              
56             =head1 ABSTRACT
57              
58             Buildable Role for Perl 5
59              
60             =cut
61              
62             =head1 SYNOPSIS
63              
64             package Vehicle;
65              
66             use Moo;
67              
68             with 'Data::Object::Role::Buildable';
69              
70             has name => (
71             is => 'rw'
72             );
73              
74             1;
75              
76             =cut
77              
78             =head1 DESCRIPTION
79              
80             This package provides methods for hooking into object construction of the
81             consuming class, e.g. handling single-arg object construction.
82              
83             =cut
84              
85             =head1 SCENARIOS
86              
87             This package supports the following scenarios:
88              
89             =cut
90              
91             =head2 buildarg
92              
93             package Car;
94              
95             use Moo;
96              
97             extends 'Vehicle';
98              
99             sub build_arg {
100             my ($class, $name) = @_;
101              
102             # do something with $name or $class ...
103              
104             return { name => $name };
105             }
106              
107             package main;
108              
109             my $car = Car->new('tesla');
110              
111             This package supports handling a C<build_arg> method, as a hook into object
112             construction, which is called and passed a single argument if a single argument
113             is passed to the constructor.
114              
115             =cut
116              
117             =head2 buildargs
118              
119             package Sedan;
120              
121             use Moo;
122              
123             extends 'Car';
124              
125             sub build_args {
126             my ($class, $args) = @_;
127              
128             # do something with $args or $class ...
129              
130             $args->{name} = ucfirst $args->{name};
131              
132             return $args;
133             }
134              
135             package main;
136              
137             my $sedan = Sedan->new('tesla');
138              
139             This package supports handling a C<build_args> method, as a hook into object
140             construction, which is called and passed a C<hashref> during object
141             construction.
142              
143             =cut
144              
145             =head2 buildself
146              
147             package Taxicab;
148              
149             use Moo;
150              
151             extends 'Sedan';
152              
153             sub build_self {
154             my ($self, $args) = @_;
155              
156             # do something with $self or $args ...
157              
158             $args->{name} = 'Toyota';
159              
160             return;
161             }
162              
163             package main;
164              
165             my $taxicab = Taxicab->new('tesla');
166              
167             This package supports handling a C<build_self> method, as a hook into object
168             construction, which is called and passed a C<hashref> during object
169             construction. Note: Manipulating the arguments doesn't effect object's
170             construction or properties.
171              
172             =cut
173              
174             =head1 AUTHOR
175              
176             Al Newkirk, C<awncorp@cpan.org>
177              
178             =head1 LICENSE
179              
180             Copyright (C) 2011-2019, Al Newkirk, et al.
181              
182             This is free software; you can redistribute it and/or modify it under the terms
183             of the The Apache License, Version 2.0, as elucidated in the L<"license
184             file"|https://github.com/iamalnewkirk/data-object-role-buildable/blob/master/LICENSE>.
185              
186             =head1 PROJECT
187              
188             L<Wiki|https://github.com/iamalnewkirk/data-object-role-buildable/wiki>
189              
190             L<Project|https://github.com/iamalnewkirk/data-object-role-buildable>
191              
192             L<Initiatives|https://github.com/iamalnewkirk/data-object-role-buildable/projects>
193              
194             L<Milestones|https://github.com/iamalnewkirk/data-object-role-buildable/milestones>
195              
196             L<Contributing|https://github.com/iamalnewkirk/data-object-role-buildable/blob/master/CONTRIBUTE.md>
197              
198             L<Issues|https://github.com/iamalnewkirk/data-object-role-buildable/issues>
199              
200             =cut