File Coverage

blib/lib/Method/Generate/Constructor.pm
Criterion Covered Total %
statement 131 131 100.0
branch 46 46 100.0
condition 24 30 80.0
subroutine 27 27 100.0
pod 0 9 0.0
total 228 243 93.8


line stmt bran cond sub pod time code
1             package Method::Generate::Constructor;
2 170     170   159560 use strict;
  170         350  
  170         5229  
3 170     170   957 use warnings;
  170         325  
  170         6641  
4              
5 170     170   47442 use Sub::Quote qw(quote_sub quotify);
  170         496114  
  170         10710  
6 170     170   1280 use Sub::Defer;
  170         407  
  170         9818  
7 170     170   2260 use Moo::_Utils qw(_getstash _getglob _linear_isa);
  170         354  
  170         8462  
8 170     170   1003 use Scalar::Util qw(weaken);
  170         406  
  170         7190  
9 170     170   1025 use Carp qw(croak);
  170         365  
  170         6783  
10 170     170   85453 use Carp::Heavy ();
  170         25090  
  170         5732  
11 170     170   5911 BEGIN { our @CARP_NOT = qw(Sub::Defer) }
12             BEGIN {
13 170     170   539 local $Moo::sification::disabled = 1;
14 170         1990 require Moo;
15 170         939 Moo->import;
16             }
17              
18             sub register_attribute_specs {
19 1462     1462 0 13434 my ($self, @new_specs) = @_;
20 1462         3780 $self->assert_constructor;
21 1458   100     5071 my $specs = $self->{attribute_specs}||={};
22 1458         3298 my $ag = $self->accessor_generator;
23 1458         5273 while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
24 1792 100       6137 if ($name =~ s/^\+//) {
25             croak "has '+${name}' given but no ${name} attribute already exists"
26 30 100       895 unless my $old_spec = $specs->{$name};
27 26         115 $ag->merge_specs($new_spec, $old_spec);
28             }
29 1788 100 100     4929 if ($new_spec->{required}
      100        
30             && !(
31             $ag->has_default($name, $new_spec)
32             || !exists $new_spec->{init_arg}
33             || defined $new_spec->{init_arg}
34             )
35             ) {
36 2         241 croak "You cannot have a required attribute (${name})"
37             . " without a default, builder, or an init_arg";
38             }
39             $new_spec->{index} = scalar keys %$specs
40 1786 100       4834 unless defined $new_spec->{index};
41 1786         5887 $specs->{$name} = $new_spec;
42             }
43 1452         8023 $self;
44             }
45              
46             sub all_attribute_specs {
47             $_[0]->{attribute_specs}
48 308     308 0 1228 }
49              
50             sub accessor_generator {
51             $_[0]->{accessor_generator}
52 2394     2394 0 5278 }
53              
54             sub construction_string {
55 586     586 0 1793 my ($self) = @_;
56             $self->{construction_string}
57 586   66     3574 ||= $self->_build_construction_string;
58             }
59              
60             sub buildall_generator {
61 34     34 0 5147 require Method::Generate::BuildAll;
62 34         218 Method::Generate::BuildAll->new;
63             }
64              
65             sub _build_construction_string {
66 466     466   971 my ($self) = @_;
67 466         1041 my $builder = $self->{construction_builder};
68 466 100       1797 $builder ? $self->$builder
69             : 'bless('
70             .$self->accessor_generator->default_construction_string
71             .', $class);'
72             }
73              
74             sub install_delayed {
75 666     666 0 22345 my ($self) = @_;
76 666         2198 $self->assert_constructor;
77 656         1481 my $package = $self->{package};
78 656         1082 my (undef, @isa) = @{_linear_isa($package)};
  656         2923  
79 656         2189 my $isa = join ',', @isa;
80 656         103360 my (undef, $from_file, $from_line) = caller(Carp::short_error_loc());
81             my $constructor = defer_sub "${package}::new" => sub {
82 508     508   161069 my (undef, @new_isa) = @{_linear_isa($package)};
  508         4231  
83 508 100       2441 if (join(',', @new_isa) ne $isa) {
84 6         17 my ($expected_new) = grep { *{_getglob($_.'::new')}{CODE} } @isa;
  8         11  
  8         26  
85 6         14 my ($found_new) = grep { *{_getglob($_.'::new')}{CODE} } @new_isa;
  12         14  
  12         28  
86 6 100 50     32 if (($found_new||'') ne ($expected_new||'')) {
      50        
87 4   50     10 $found_new ||= 'none';
88 4   50     12 $expected_new ||= 'none';
89 4         588 croak "Expected parent constructor of $package to be"
90             . " $expected_new, but found $found_new: changing the inheritance"
91             . " chain (\@ISA) at runtime (after $from_file line $from_line) is unsupported";
92             }
93             }
94              
95             my $constructor = $self->generate_method(
96 504         3191 $package, 'new', $self->{attribute_specs}, { no_install => 1, no_defer => 1 }
97             );
98 504         434071 $self->{inlined} = 1;
99 504         2338 weaken($self->{constructor} = $constructor);
100 504         1788 $constructor;
101 656         7287 };
102 656         41919 $self->{inlined} = 0;
103 656         2376 weaken($self->{constructor} = $constructor);
104 656         1778 $self;
105             }
106              
107             sub current_constructor {
108 2134     2134 0 6117 my ($self, $package) = @_;
109 2134         3374 return *{_getglob("${package}::new")}{CODE};
  2134         7002  
110             }
111              
112             sub assert_constructor {
113 2134     2134 0 3780 my ($self) = @_;
114 2134 100       6081 my $package = $self->{package} or return 1;
115 2130 100       4426 my $current = $self->current_constructor($package)
116             or return 1;
117             my $constructor = $self->{constructor}
118 1478 100       5937 or croak "Unknown constructor for $package already exists";
119 1468 100       4237 croak "Constructor for $package has been replaced with an unknown sub"
120             if $constructor != $current;
121             croak "Constructor for $package has been inlined and cannot be updated"
122 1466 100       4859 if $self->{inlined};
123             }
124              
125             sub generate_method {
126 508     508 0 5036 my ($self, $into, $name, $spec, $quote_opts) = @_;
127             $quote_opts = {
128 508 100       973 %{$quote_opts||{}},
  508         2925  
129             package => $into,
130             };
131 508         2935 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
132 1458         2790 $spec->{$no_init}{init_arg} = $no_init;
133             }
134 508         1686 local $self->{captures} = {};
135              
136 508         4257 my $into_buildargs = $into->can('BUILDARGS');
137              
138 508 100 100     1807 my $body
    100          
139             = ' my $invoker = CORE::shift();'."\n"
140             . ' my $class = CORE::ref($invoker) ? CORE::ref($invoker) : $invoker;'."\n"
141             . $self->_handle_subconstructor($into, $name)
142             . ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS
143             ? $self->_generate_args_via_buildargs
144             : $self->_generate_args
145             )
146             . $self->_check_required($spec)
147             . ' my $new = '.$self->construction_string.";\n"
148             . $self->_assign_new($spec)
149             . ( $into->can('BUILD')
150             ? $self->buildall_generator->buildall_body_for( $into, '$new', '$args' )
151             : ''
152             )
153             . ' return $new;'."\n";
154              
155 508 100       4611 if ($into->can('DEMOLISH')) {
156 8         2820 require Method::Generate::DemolishAll;
157 8         66 Method::Generate::DemolishAll->new->generate_method($into);
158             }
159             quote_sub
160             "${into}::${name}" => $body,
161 508   50     4349 $self->{captures}, $quote_opts||{}
162             ;
163             }
164              
165             sub _handle_subconstructor {
166 508     508   1601 my ($self, $into, $name) = @_;
167 508 100       1740 if (my $gen = $self->{subconstructor_handler}) {
168 504         2111 ' if ($class ne '.quotify($into).') {'."\n".
169             $gen.
170             ' }'."\n";
171             } else {
172 4         23 ''
173             }
174             }
175              
176             sub _cap_call {
177 1560     1560   3221 my ($self, $code, $captures) = @_;
178 1560 100       4911 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
  1558         3037  
179 1560         13513 $code;
180             }
181              
182             sub _generate_args_via_buildargs {
183 24     24   533 my ($self) = @_;
184 24         102 q{ my $args = $class->BUILDARGS(@_);}."\n"
185             .q{ Carp::croak("BUILDARGS did not return a hashref") unless CORE::ref($args) eq 'HASH';}
186             ."\n";
187             }
188              
189             # inlined from Moo::Object - update that first.
190             sub _generate_args {
191 484     484   10987 my ($self) = @_;
192 484         2256 return <<'_EOA';
193             my $args = scalar @_ == 1
194             ? CORE::ref $_[0] eq 'HASH'
195             ? { %{ $_[0] } }
196             : Carp::croak("Single parameters to new() must be a HASH ref"
197             . " data => ". $_[0])
198             : @_ % 2
199             ? Carp::croak("The new() method for $class expects a hash reference or a"
200             . " key/value list. You passed an odd number of arguments")
201             : {@_}
202             ;
203             _EOA
204              
205             }
206              
207             sub _assign_new {
208 508     508   1212 my ($self, $spec) = @_;
209 508         1233 my $ag = $self->accessor_generator;
210 508         994 my %test;
211 508         1986 NAME: foreach my $name (sort keys %$spec) {
212 1570         2535 my $attr_spec = $spec->{$name};
213             next NAME unless defined($attr_spec->{init_arg})
214 1570 100 100     3870 or $ag->has_eager_default($name, $attr_spec);
215 1558         3421 $test{$name} = $attr_spec->{init_arg};
216             }
217             join '', map {
218 508         2134 my $arg = $test{$_};
  1558         2756  
219 1558         3478 my $arg_key = quotify($arg);
220 1558 100       13616 my $test = defined $arg ? "exists \$args->{$arg_key}" : undef;
221 1558 100       4114 my $source = defined $arg ? "\$args->{$arg_key}" : undef;
222 1558         2517 my $attr_spec = $spec->{$_};
223 1558         4268 $self->_cap_call($ag->generate_populate_set(
224             '$new', $_, $attr_spec, $source, $test, $arg,
225             ));
226             } sort keys %test;
227             }
228              
229             sub _check_required {
230 508     508   1247 my ($self, $spec) = @_;
231             my @required_init =
232             map $spec->{$_}{init_arg},
233             grep {
234 508         4267 my $s = $spec->{$_}; # ignore required if default or builder set
  1570         2534  
235             $s->{required} and not($s->{builder} or exists $s->{default})
236 1570 100 100     4468 } sort keys %$spec;
237 508 100       3393 return '' unless @required_init;
238 36         139 ' if (my @missing = grep !exists $args->{$_}, '
239             .join(', ', map quotify($_), @required_init).') {'."\n"
240             .q{ Carp::croak("Missing required arguments: ".CORE::join(', ', sort @missing));}."\n"
241             ." }\n";
242             }
243              
244             # bootstrap our own constructor
245             sub new {
246             my $class = shift;
247             delete _getstash(__PACKAGE__)->{new};
248             bless $class->BUILDARGS(@_), $class;
249             }
250             Moo->_constructor_maker_for(__PACKAGE__)
251             ->register_attribute_specs(
252             attribute_specs => {
253             is => 'ro',
254             reader => 'all_attribute_specs',
255             },
256             accessor_generator => { is => 'ro' },
257             construction_string => { is => 'lazy' },
258             construction_builder => { is => 'bare' },
259             subconstructor_handler => { is => 'ro' },
260             package => { is => 'bare' },
261             );
262             if ($INC{'Moo/HandleMoose.pm'} && !$Moo::sification::disabled) {
263             Moo::HandleMoose::inject_fake_metaclass_for(__PACKAGE__);
264             }
265              
266             1;