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   163542 use strict;
  170         366  
  170         5632  
3 170     170   1038 use warnings;
  170         338  
  170         5564  
4              
5 170     170   51479 use Sub::Quote qw(quote_sub quotify);
  170         521994  
  170         10890  
6 170     170   1328 use Sub::Defer;
  170         446  
  170         10165  
7 170     170   2591 use Moo::_Utils qw(_getstash _getglob _linear_isa);
  170         373  
  170         8870  
8 170     170   1075 use Scalar::Util qw(weaken);
  170         435  
  170         7444  
9 170     170   1105 use Carp qw(croak);
  170         400  
  170         7026  
10 170     170   89465 use Carp::Heavy ();
  170         26362  
  170         6075  
11 170     170   6142 BEGIN { our @CARP_NOT = qw(Sub::Defer) }
12             BEGIN {
13 170     170   571 local $Moo::sification::disabled = 1;
14 170         2072 require Moo;
15 170         1046 Moo->import;
16             }
17              
18             sub register_attribute_specs {
19 1462     1462 0 13651 my ($self, @new_specs) = @_;
20 1462         3957 $self->assert_constructor;
21 1458   100     5274 my $specs = $self->{attribute_specs}||={};
22 1458         3265 my $ag = $self->accessor_generator;
23 1458         5445 while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
24 1792 100       4975 if ($name =~ s/^\+//) {
25             croak "has '+${name}' given but no ${name} attribute already exists"
26 30 100       914 unless my $old_spec = $specs->{$name};
27 26         108 $ag->merge_specs($new_spec, $old_spec);
28             }
29 1788 100 100     4635 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         206 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       5074 unless defined $new_spec->{index};
41 1786         6213 $specs->{$name} = $new_spec;
42             }
43 1452         9702 $self;
44             }
45              
46             sub all_attribute_specs {
47             $_[0]->{attribute_specs}
48 308     308 0 1384 }
49              
50             sub accessor_generator {
51             $_[0]->{accessor_generator}
52 2394     2394 0 5482 }
53              
54             sub construction_string {
55 586     586 0 1853 my ($self) = @_;
56             $self->{construction_string}
57 586   66     3504 ||= $self->_build_construction_string;
58             }
59              
60             sub buildall_generator {
61 34     34 0 5895 require Method::Generate::BuildAll;
62 34         233 Method::Generate::BuildAll->new;
63             }
64              
65             sub _build_construction_string {
66 466     466   1001 my ($self) = @_;
67 466         1018 my $builder = $self->{construction_builder};
68 466 100       1943 $builder ? $self->$builder
69             : 'bless('
70             .$self->accessor_generator->default_construction_string
71             .', $class);'
72             }
73              
74             sub install_delayed {
75 666     666 0 23709 my ($self) = @_;
76 666         2318 $self->assert_constructor;
77 656         1516 my $package = $self->{package};
78 656         1100 my (undef, @isa) = @{_linear_isa($package)};
  656         3071  
79 656         2415 my $isa = join ',', @isa;
80 656         107992 my (undef, $from_file, $from_line) = caller(Carp::short_error_loc());
81             my $constructor = defer_sub "${package}::new" => sub {
82 508     508   169490 my (undef, @new_isa) = @{_linear_isa($package)};
  508         4037  
83 508 100       2652 if (join(',', @new_isa) ne $isa) {
84 6         17 my ($expected_new) = grep { *{_getglob($_.'::new')}{CODE} } @isa;
  8         14  
  8         35  
85 6         14 my ($found_new) = grep { *{_getglob($_.'::new')}{CODE} } @new_isa;
  12         17  
  12         35  
86 6 100 50     35 if (($found_new||'') ne ($expected_new||'')) {
      50        
87 4   50     23 $found_new ||= 'none';
88 4   50     13 $expected_new ||= 'none';
89 4         640 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         3153 $package, 'new', $self->{attribute_specs}, { no_install => 1, no_defer => 1 }
97             );
98 504         448980 $self->{inlined} = 1;
99 504         2408 weaken($self->{constructor} = $constructor);
100 504         1770 $constructor;
101 656         7457 };
102 656         43116 $self->{inlined} = 0;
103 656         2547 weaken($self->{constructor} = $constructor);
104 656         1898 $self;
105             }
106              
107             sub current_constructor {
108 2134     2134 0 4861 my ($self, $package) = @_;
109 2134         4778 return *{_getglob("${package}::new")}{CODE};
  2134         7422  
110             }
111              
112             sub assert_constructor {
113 2134     2134 0 4069 my ($self) = @_;
114 2134 100       6206 my $package = $self->{package} or return 1;
115 2130 100       4516 my $current = $self->current_constructor($package)
116             or return 1;
117             my $constructor = $self->{constructor}
118 1478 100       5843 or croak "Unknown constructor for $package already exists";
119 1468 100       4214 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       4874 if $self->{inlined};
123             }
124              
125             sub generate_method {
126 508     508 0 5005 my ($self, $into, $name, $spec, $quote_opts) = @_;
127             $quote_opts = {
128 508 100       992 %{$quote_opts||{}},
  508         3033  
129             package => $into,
130             };
131 508         2941 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
132 1458         2911 $spec->{$no_init}{init_arg} = $no_init;
133             }
134 508         1691 local $self->{captures} = {};
135              
136 508         4322 my $into_buildargs = $into->can('BUILDARGS');
137              
138 508 100 100     1886 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       4764 if ($into->can('DEMOLISH')) {
156 8         3366 require Method::Generate::DemolishAll;
157 8         77 Method::Generate::DemolishAll->new->generate_method($into);
158             }
159             quote_sub
160             "${into}::${name}" => $body,
161 508   50     4679 $self->{captures}, $quote_opts||{}
162             ;
163             }
164              
165             sub _handle_subconstructor {
166 508     508   1298 my ($self, $into, $name) = @_;
167 508 100       1672 if (my $gen = $self->{subconstructor_handler}) {
168 504         2075 ' if ($class ne '.quotify($into).') {'."\n".
169             $gen.
170             ' }'."\n";
171             } else {
172 4         19 ''
173             }
174             }
175              
176             sub _cap_call {
177 1560     1560   3272 my ($self, $code, $captures) = @_;
178 1560 100       5064 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
  1558         3249  
179 1560         14453 $code;
180             }
181              
182             sub _generate_args_via_buildargs {
183 24     24   528 my ($self) = @_;
184 24         90 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   11241 my ($self) = @_;
192 484         2354 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   1222 my ($self, $spec) = @_;
209 508         1246 my $ag = $self->accessor_generator;
210 508         979 my %test;
211 508         2104 NAME: foreach my $name (sort keys %$spec) {
212 1570         2653 my $attr_spec = $spec->{$name};
213             next NAME unless defined($attr_spec->{init_arg})
214 1570 100 100     4092 or $ag->has_eager_default($name, $attr_spec);
215 1558         3548 $test{$name} = $attr_spec->{init_arg};
216             }
217             join '', map {
218 508         2267 my $arg = $test{$_};
  1558         3015  
219 1558         3555 my $arg_key = quotify($arg);
220 1558 100       14060 my $test = defined $arg ? "exists \$args->{$arg_key}" : undef;
221 1558 100       3974 my $source = defined $arg ? "\$args->{$arg_key}" : undef;
222 1558         2692 my $attr_spec = $spec->{$_};
223 1558         4401 $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   1294 my ($self, $spec) = @_;
231             my @required_init =
232             map $spec->{$_}{init_arg},
233             grep {
234 508         2808 my $s = $spec->{$_}; # ignore required if default or builder set
  1570         4199  
235             $s->{required} and not($s->{builder} or exists $s->{default})
236 1570 100 100     4701 } sort keys %$spec;
237 508 100       3221 return '' unless @required_init;
238 36         196 ' 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;