From: Matt S Trout Date: Thu, 3 May 2012 18:29:27 +0000 (+0000) Subject: preserve attribute ordering X-Git-Tag: v0.091002~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoo.git;a=commitdiff_plain;h=57d402efbbb15ffa3ed5415471b4e321f8e1fb80 preserve attribute ordering --- diff --git a/Changes b/Changes index ef33b2e..e29d48c 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - preserve attribute ordering - factor out accessor generation code a bit more to enable extension 0.091001 - 2012-05-02 diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index d24a457..1aa8225 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -11,7 +11,7 @@ sub register_attribute_specs { my $specs = $self->{attribute_specs}||={}; while (my ($name, $new_spec) = splice @new_specs, 0, 2) { $new_spec->{index} = scalar keys %$specs - unless exists $new_spec->{index}; + unless defined $new_spec->{index}; $specs->{$name} = $new_spec; } $self; diff --git a/lib/Moo/HandleMoose.pm b/lib/Moo/HandleMoose.pm index e5cef0f..e63e0be 100644 --- a/lib/Moo/HandleMoose.pm +++ b/lib/Moo/HandleMoose.pm @@ -41,12 +41,18 @@ sub inject_real_metaclass_for { return Class::MOP::get_metaclass_by_name($name) if $DID_INJECT{$name}; require Moose; require Moo; require Moo::Role; Class::MOP::remove_metaclass_by_name($name); - my ($am_role, $meta, $attr_specs) = do { + my ($am_role, $meta, $attr_specs, $attr_order) = do { if (my $info = $Moo::Role::INFO{$name}) { - (1, Moose::Meta::Role->initialize($name), $info->{attributes}) + my @attr_info = @{$info->{attributes}||[]}; + (1, Moose::Meta::Role->initialize($name), + { @attr_info }, + [ @attr_info[grep !($_ % 2), 0..$#attr_info] ] + ) } else { my $specs = Moo->_constructor_maker_for($name)->all_attribute_specs; - (0, Moose::Meta::Class->initialize($name), $specs); + (0, Moose::Meta::Class->initialize($name), $specs, + [ sort { $specs->{$a}{index} <=> $specs->{$b}{index} } keys %$specs ] + ); } }; my %methods = %{Role::Tiny->_concrete_methods_of($name)}; @@ -56,7 +62,7 @@ sub inject_real_metaclass_for { { # This local is completely not required for roles but harmless local @{_getstash($name)}{keys %methods}; - foreach my $name (keys %$attr_specs) { + foreach my $name (@$attr_order) { my %spec = %{$attr_specs->{$name}}; delete $spec{index}; $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp'; diff --git a/lib/Moo/Role.pm b/lib/Moo/Role.pm index 914f2b7..efc17e9 100644 --- a/lib/Moo/Role.pm +++ b/lib/Moo/Role.pm @@ -20,7 +20,7 @@ sub import { require Method::Generate::Accessor; Method::Generate::Accessor->new })->generate_method($target, $name, \%spec); - $INFO{$target}{attributes}{$name} = \%spec; + push @{$INFO{$target}{attributes}||=[]}, $name, \%spec; }; if ($INC{'Moo/HandleMoose.pm'}) { Moo::HandleMoose::inject_fake_metaclass_for($target); @@ -40,9 +40,9 @@ sub _inhale_if_moose { map +($_->name => 1), $meta->calculate_all_roles }; $INFO{$role}{requires} = [ $meta->get_required_method_list ]; - $INFO{$role}{attributes} = { + $INFO{$role}{attributes} = [ map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list - }; + ]; my $mods = $INFO{$role}{modifiers} = []; foreach my $type (qw(before after around)) { my $map = $meta->${\"get_${type}_method_modifiers_map"}; @@ -61,13 +61,13 @@ sub _inhale_if_moose { sub _make_accessors_if_moose { my ($self, $role, $target) = @_; if ($INFO{$role}{inhaled_from_moose}) { - if (my $attrs = $INFO{$role}{attributes}) { + if (my @attrs = @{$INFO{$role}{attributes}||[]}) { my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do { require Method::Generate::Accessor; Method::Generate::Accessor->new }); - foreach my $name (keys %{$attrs}) { - $acc_gen->generate_method($target, $name, $attrs->{$name}); + while (my ($name, $spec) = splice @attrs, 0, 2) { + $acc_gen->generate_method($target, $name, $spec); } } } @@ -103,7 +103,7 @@ sub create_class_with_roles { $Moo::MAKERS{$new_name} = {}; $me->_handle_constructor( - $new_name, { map %{$INFO{$_}{attributes}||{}}, @roles }, $superclass + $new_name, [ map @{$INFO{$_}{attributes}||{}}, @roles ], $superclass ); return $new_name; @@ -124,14 +124,14 @@ sub _install_single_modifier { sub _handle_constructor { my ($me, $to, $attr_info, $superclass) = @_; - return unless $attr_info && keys %$attr_info; + return unless $attr_info && @$attr_info; if ($INFO{$to}) { - @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info; + push @{$INFO{$to}{attributes}||=[]}, @$attr_info; } else { # only fiddle with the constructor if the target is a Moo class if ($INC{"Moo.pm"} and my $con = Moo->_constructor_maker_for($to, $superclass)) { - $con->register_attribute_specs(%$attr_info); + $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info); } } }