File Coverage

blib/lib/Method/Generate/Constructor/Role/RetroClassOnlyConstructor.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Method::Generate::Constructor::Role::RetroClassOnlyConstructor;
2              
3             # ABSTRACT: a role to make Moo constructors class-only methods.
4              
5 2     2   1100 use Moo::Role;
  2         4  
  2         12  
6 2     2   718 use Sub::Quote;
  2         3  
  2         725  
7              
8             {
9             $Method::Generate::Constructor::Role::RetroClassOnlyConstructor::VERSION = 'v0.1';
10             }
11              
12             # since we can't break into it like the proposed patch, just replace it
13             around generate_method => sub {
14             my $orig = shift;
15             my ($self, $into, $name, $spec, $quote_opts) = @_;
16             foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
17             $spec->{$no_init}{init_arg} = $no_init;
18             }
19             local $self->{captures} = {};
20              
21             # this is the only change that is made.
22             my $body = qq{
23             # Method::Generate::Constructor::Role::RetroClassOnlyConstructor
24             require Carp;
25             Carp::croak "'$into->$name' must be called as a class method"
26             if ref(\$_[0]);
27              
28             };
29              
30             $body .= ' my $class = shift;'."\n"
31             .' $class = ref($class) if ref($class);'."\n";
32             $body .= $self->_handle_subconstructor($into, $name);
33             my $into_buildargs = $into->can('BUILDARGS');
34             if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
35             $body .= $self->_generate_args_via_buildargs;
36             } else {
37             $body .= $self->_generate_args;
38             }
39             $body .= $self->_check_required($spec);
40             $body .= ' my $new = '.$self->construction_string.";\n";
41             $body .= $self->_assign_new($spec);
42             if ($into->can('BUILD')) {
43             $body .= $self->buildall_generator->buildall_body_for(
44             $into, '$new', '$args'
45             );
46             }
47             $body .= ' return $new;'."\n";
48             if ($into->can('DEMOLISH')) {
49             require Method::Generate::DemolishAll;
50             Method::Generate::DemolishAll->new->generate_method($into);
51             }
52             quote_sub
53             "${into}::${name}" => $body,
54             $self->{captures}, $quote_opts||{}
55             ;
56             };
57              
58             1;
59              
60             __END__