1 package Moo::HandleMoose;
11 sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; }
15 inject_fake_metaclass_for($_)
16 for grep $_ ne 'Moo::Object', do { no warnings 'once'; keys %Moo::MAKERS };
17 inject_fake_metaclass_for($_) for keys %Moo::Role::INFO;
18 require Moose::Meta::Method::Constructor;
19 @Moo::HandleMoose::FakeConstructor::ISA = 'Moose::Meta::Method::Constructor';
22 sub inject_fake_metaclass_for {
25 Class::MOP::store_metaclass_by_name(
26 $name, bless({ name => $name }, 'Moo::HandleMoose::FakeMetaClass')
33 package Moo::HandleMoose::FakeConstructor;
35 sub _uninlined_body { \&Moose::Object::new }
39 sub inject_real_metaclass_for {
41 return Class::MOP::get_metaclass_by_name($name) if $DID_INJECT{$name};
42 require Moose; require Moo; require Moo::Role;
43 Class::MOP::remove_metaclass_by_name($name);
44 my ($am_role, $meta, $attr_specs) = do {
45 if (my $info = $Moo::Role::INFO{$name}) {
46 (1, Moose::Meta::Role->initialize($name), $info->{attributes})
48 my $specs = Moo->_constructor_maker_for($name)->all_attribute_specs;
49 (0, Moose::Meta::Class->initialize($name), $specs);
52 my %methods = %{Role::Tiny->_concrete_methods_of($name)};
53 # needed to ensure the method body is stable and get things named
54 Sub::Defer::undefer_sub($_) for grep defined, values %methods;
57 # This local is completely not required for roles but harmless
58 local @{_getstash($name)}{keys %methods};
59 foreach my $name (keys %$attr_specs) {
60 my %spec = %{$attr_specs->{$name}};
61 $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
62 delete $spec{asserter};
63 if (my $isa = $spec{isa}) {
65 if (my $mapped = $TYPE_MAP{$isa}) {
68 Moose::Meta::TypeConstraint->new(
69 constraint => sub { eval { &$isa; 1 } }
73 die "Aaaargh" if $spec{coerce};
74 } elsif (my $coerce = $spec{coerce}) {
75 my $attr = perlstring($name);
76 my $tc = Moose::Meta::TypeConstraint->new(
77 constraint => sub { die "This is not going to work" },
79 'my $r = $_[42]{'.$attr.'}; $_[42]{'.$attr.'} = 1; $r'
82 $tc->coercion(Moose::Meta::TypeCoercion->new)
83 ->_compiled_type_coercion($coerce);
87 push @attrs, $meta->add_attribute($name => %spec);
91 my $info = $Moo::Role::INFO{$name};
92 $meta->add_required_methods(@{$info->{requires}});
93 foreach my $modifier (@{$info->{modifiers}}) {
94 my ($type, @args) = @$modifier;
95 $meta->${\"add_${type}_method_modifier"}(@args);
98 foreach my $attr (@attrs) {
99 foreach my $method (@{$attr->associated_methods}) {
100 $method->{body} = $name->can($method->name);
104 $meta->find_method_by_name('new'),
105 'Moo::HandleMoose::FakeConstructor',
108 $meta->add_role(Class::MOP::class_of($_))
109 for do { no warnings 'once'; keys %{$Role::Tiny::APPLIED_TO{$name}} };
110 $DID_INJECT{$name} = 1;
115 package Moo::HandleMoose::FakeMetaClass;
120 my ($meth) = (our $AUTOLOAD =~ /([^:]+)$/);
121 Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->$meth(@_)
124 Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->can(@_)
127 Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->isa(@_)