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   146576 use strict;
  170         368  
  170         5289  
3 170     170   885 use warnings;
  170         363  
  170         5565  
4              
5 170     170   44553 use Sub::Quote qw(quote_sub quotify);
  170         511795  
  170         9374  
6 170     170   1174 use Sub::Defer;
  170         407  
  170         8697  
7 170     170   1901 use Moo::_Utils qw(_getstash _getglob _linear_isa);
  170         378  
  170         8524  
8 170     170   1053 use Scalar::Util qw(weaken);
  170         423  
  170         6709  
9 170     170   1050 use Carp qw(croak);
  170         430  
  170         6811  
10 170     170   74092 use Carp::Heavy ();
  170         26433  
  170         5871  
11 170     170   6140 BEGIN { our @CARP_NOT = qw(Sub::Defer) }
12             BEGIN {
13 170     170   589 local $Moo::sification::disabled = 1;
14 170         1976 require Moo;
15 170         957 Moo->import;
16             }
17              
18             sub register_attribute_specs {
19 1462     1462 0 13041 my ($self, @new_specs) = @_;
20 1462         3680 $self->assert_constructor;
21 1458   100     4880 my $specs = $self->{attribute_specs}||={};
22 1458         3115 my $ag = $self->accessor_generator;
23 1458         5238 while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
24 1792 100       4757 if ($name =~ s/^\+//) {
25 30 100       977 my $old_spec = $specs->{$name}
26             or croak "has '+${name}' given but no ${name} attribute already exists";
27 26         106 $ag->merge_specs($new_spec, $old_spec);
28             }
29 1788 100 100     4525 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         223 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       5016 unless defined $new_spec->{index};
41 1786         5958 $specs->{$name} = $new_spec;
42             }
43 1452         7844 $self;
44             }
45              
46             sub all_attribute_specs {
47             $_[0]->{attribute_specs}
48 308     308 0 1198 }
49              
50             sub accessor_generator {
51             $_[0]->{accessor_generator}
52 2394     2394 0 5280 }
53              
54             sub construction_string {
55 586     586 0 1933 my ($self) = @_;
56             $self->{construction_string}
57 586   66     3278 ||= $self->_build_construction_string;
58             }
59              
60             sub buildall_generator {
61 34     34 0 4713 require Method::Generate::BuildAll;
62 34         210 Method::Generate::BuildAll->new;
63             }
64              
65             sub _build_construction_string {
66 466     466   1085 my ($self) = @_;
67 466         1094 my $builder = $self->{construction_builder};
68 466 100       1708 $builder ? $self->$builder
69             : 'bless('
70             .$self->accessor_generator->default_construction_string
71             .', $class);'
72             }
73              
74             sub install_delayed {
75 666     666 0 22546 my ($self) = @_;
76 666         1978 $self->assert_constructor;
77 656         1460 my $package = $self->{package};
78 656         1097 my (undef, @isa) = @{_linear_isa($package)};
  656         2753  
79 656         2169 my $isa = join ',', @isa;
80 656         102660 my (undef, $from_file, $from_line) = caller(Carp::short_error_loc());
81             my $constructor = defer_sub "${package}::new" => sub {
82 508     508   172684 my (undef, @new_isa) = @{_linear_isa($package)};
  508         2370  
83 508 100       2328 if (join(',', @new_isa) ne $isa) {
84 6         18 my ($expected_new) = grep { *{_getglob($_.'::new')}{CODE} } @isa;
  8         14  
  8         29  
85 6         15 my ($found_new) = grep { *{_getglob($_.'::new')}{CODE} } @new_isa;
  12         17  
  12         31  
86 6 100 50     35 if (($found_new||'') ne ($expected_new||'')) {
      50        
87 4   50     14 $found_new ||= 'none';
88 4   50     12 $expected_new ||= 'none';
89 4         650 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         2744 $package, 'new', $self->{attribute_specs}, { no_install => 1, no_defer => 1 }
97             );
98 504         438623 $self->{inlined} = 1;
99 504         2260 weaken($self->{constructor} = $constructor);
100 504         1716 $constructor;
101 656         6882 };
102 656         40190 $self->{inlined} = 0;
103 656         2384 weaken($self->{constructor} = $constructor);
104 656         1704 $self;
105             }
106              
107             sub current_constructor {
108 2134     2134 0 5024 my ($self, $package) = @_;
109 2134         2963 return *{_getglob("${package}::new")}{CODE};
  2134         6848  
110             }
111              
112             sub assert_constructor {
113 2134     2134 0 3759 my ($self) = @_;
114 2134 100       6287 my $package = $self->{package} or return 1;
115 2130 100       4388 my $current = $self->current_constructor($package)
116             or return 1;
117             my $constructor = $self->{constructor}
118 1478 100       7246 or croak "Unknown constructor for $package already exists";
119 1468 100       4056 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       4776 if $self->{inlined};
123             }
124              
125             sub generate_method {
126 508     508 0 5415 my ($self, $into, $name, $spec, $quote_opts) = @_;
127             $quote_opts = {
128 508 100       889 %{$quote_opts||{}},
  508         2853  
129             package => $into,
130             };
131 508         2799 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
132 1458         2882 $spec->{$no_init}{init_arg} = $no_init;
133             }
134 508         1766 local $self->{captures} = {};
135              
136 508         4150 my $into_buildargs = $into->can('BUILDARGS');
137              
138 508 100 100     1749 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       4619 if ($into->can('DEMOLISH')) {
156 8         3066 require Method::Generate::DemolishAll;
157 8         65 Method::Generate::DemolishAll->new->generate_method($into);
158             }
159             quote_sub
160             "${into}::${name}" => $body,
161 508   50     4115 $self->{captures}, $quote_opts||{}
162             ;
163             }
164              
165             sub _handle_subconstructor {
166 508     508   1229 my ($self, $into, $name) = @_;
167 508 100       3137 if (my $gen = $self->{subconstructor_handler}) {
168 504         1879 ' if ($class ne '.quotify($into).') {'."\n".
169             $gen.
170             ' }'."\n";
171             } else {
172 4         21 ''
173             }
174             }
175              
176             sub _cap_call {
177 1560     1560   3258 my ($self, $code, $captures) = @_;
178 1560 100       4890 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
  1558         3197  
179 1560         13589 $code;
180             }
181              
182             sub _generate_args_via_buildargs {
183 24     24   471 my ($self) = @_;
184 24         98 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   10476 my ($self) = @_;
192 484         1959 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         1115 my $ag = $self->accessor_generator;
210 508         955 my %test;
211 508         1960 NAME: foreach my $name (sort keys %$spec) {
212 1570         2464 my $attr_spec = $spec->{$name};
213             next NAME unless defined($attr_spec->{init_arg})
214 1570 100 100     3867 or $ag->has_eager_default($name, $attr_spec);
215 1558         3519 $test{$name} = $attr_spec->{init_arg};
216             }
217             join '', map {
218 508         2093 my $arg = $test{$_};
  1558         2986  
219 1558         3507 my $arg_key = quotify($arg);
220 1558 100       14243 my $test = defined $arg ? "exists \$args->{$arg_key}" : undef;
221 1558 100       3804 my $source = defined $arg ? "\$args->{$arg_key}" : undef;
222 1558         2575 my $attr_spec = $spec->{$_};
223 1558         4153 $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   1240 my ($self, $spec) = @_;
231             my @required_init =
232             map $spec->{$_}{init_arg},
233             grep {
234 508         2440 my $s = $spec->{$_}; # ignore required if default or builder set
  1570         2614  
235             $s->{required} and not($s->{builder} or exists $s->{default})
236 1570 100 100     4579 } sort keys %$spec;
237 508 100       2941 return '' unless @required_init;
238 36         161 ' 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;