preserve attribute ordering
[gitmo/Moo.git] / lib / Moo / HandleMoose.pm
1 package Moo::HandleMoose;
2
3 use strictures 1;
4 use Moo::_Utils;
5 use B qw(perlstring);
6
7 our %TYPE_MAP;
8
9 our $SETUP_DONE;
10
11 sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; }
12
13 sub inject_all {
14   require Class::MOP;
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';
20 }
21
22 sub inject_fake_metaclass_for {
23   my ($name) = @_;
24   require Class::MOP;
25   Class::MOP::store_metaclass_by_name(
26     $name, bless({ name => $name }, 'Moo::HandleMoose::FakeMetaClass')
27   );
28 }
29
30 our %DID_INJECT;
31
32 {
33   package Moo::HandleMoose::FakeConstructor;
34
35   sub _uninlined_body { \&Moose::Object::new }
36 }
37     
38
39 sub inject_real_metaclass_for {
40   my ($name) = @_;
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, $attr_order) = do {
45     if (my $info = $Moo::Role::INFO{$name}) {
46       my @attr_info = @{$info->{attributes}||[]};
47       (1, Moose::Meta::Role->initialize($name),
48        { @attr_info },
49        [ @attr_info[grep !($_ % 2), 0..$#attr_info] ]
50       )
51     } else {
52       my $specs = Moo->_constructor_maker_for($name)->all_attribute_specs;
53       (0, Moose::Meta::Class->initialize($name), $specs,
54        [ sort { $specs->{$a}{index} <=> $specs->{$b}{index} } keys %$specs ]
55       );
56     }
57   };
58   my %methods = %{Role::Tiny->_concrete_methods_of($name)};
59   # needed to ensure the method body is stable and get things named
60   Sub::Defer::undefer_sub($_) for grep defined, values %methods;
61   my @attrs;
62   {
63     # This local is completely not required for roles but harmless
64     local @{_getstash($name)}{keys %methods};
65     foreach my $name (@$attr_order) {
66       my %spec = %{$attr_specs->{$name}};
67       delete $spec{index};
68       $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
69       delete $spec{asserter};
70       if (my $isa = $spec{isa}) {
71         $spec{isa} = do {
72           if (my $mapped = $TYPE_MAP{$isa}) {
73             $mapped->();
74           } else {
75             Moose::Meta::TypeConstraint->new(
76               constraint => sub { eval { &$isa; 1 } }
77             );
78           }
79         };
80         die "Aaaargh" if $spec{coerce};
81       } elsif (my $coerce = $spec{coerce}) {
82         my $attr = perlstring($name);
83         my $tc = Moose::Meta::TypeConstraint->new(
84                    constraint => sub { die "This is not going to work" },
85                    inlined => sub {
86                       'my $r = $_[42]{'.$attr.'}; $_[42]{'.$attr.'} = 1; $r'
87                    },
88                  );
89          $tc->coercion(Moose::Meta::TypeCoercion->new)
90             ->_compiled_type_coercion($coerce);
91          $spec{isa} = $tc;
92          $spec{coerce} = 1;
93       }
94       push @attrs, $meta->add_attribute($name => %spec);
95     }
96   }
97   if ($am_role) {
98     my $info = $Moo::Role::INFO{$name};
99     $meta->add_required_methods(@{$info->{requires}});
100     foreach my $modifier (@{$info->{modifiers}}) {
101       my ($type, @args) = @$modifier;
102       $meta->${\"add_${type}_method_modifier"}(@args);
103     }
104   } else {
105     foreach my $attr (@attrs) {
106       foreach my $method (@{$attr->associated_methods}) {
107         $method->{body} = $name->can($method->name);
108       }
109     }
110     bless(
111       $meta->find_method_by_name('new'),
112       'Moo::HandleMoose::FakeConstructor',
113     );
114   }
115   $meta->add_role(Class::MOP::class_of($_))
116     for do { no warnings 'once'; keys %{$Role::Tiny::APPLIED_TO{$name}} };
117   $DID_INJECT{$name} = 1;
118   $meta;
119 }
120
121 {
122   package Moo::HandleMoose::FakeMetaClass;
123
124   sub DESTROY { }
125
126   sub AUTOLOAD {
127     my ($meth) = (our $AUTOLOAD =~ /([^:]+)$/);
128     Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->$meth(@_)
129   }
130   sub can {
131     Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->can(@_)
132   }
133   sub isa {
134     Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->isa(@_)
135   }
136 }
137
138 1;