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