X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoo.git;a=blobdiff_plain;f=lib%2FMoo%2FRole.pm;h=61d66da20de0ec1b3d1c7e38b0f3cd7b08ad97aa;hp=56245857161cb7b326f2f91b69c7fb4dff482905;hb=141b507ace5957c098c52ceb7afaf21d5928a02d;hpb=db10ae2d7e82cc64f8f7b93f34bc924b7125c062 diff --git a/lib/Moo/Role.pm b/lib/Moo/Role.pm index 5624585..61d66da 100644 --- a/lib/Moo/Role.pm +++ b/lib/Moo/Role.pm @@ -10,53 +10,146 @@ BEGIN { *INFO = \%Role::Tiny::INFO } our %INFO; +sub _install_tracked { + my ($target, $name, $code) = @_; + $INFO{$target}{exports}{$name} = $code; + _install_coderef "${target}::${name}" => "Moo::Role::${name}" => $code; +} + sub import { my $target = caller; + my ($me) = @_; strictures->import; return if $INFO{$target}; # already exported into this package + $INFO{$target} = {}; # get symbol table reference my $stash = do { no strict 'refs'; \%{"${target}::"} }; - _install_coderef "${target}::has" => sub { - my ($name, %spec) = @_; - ($INFO{$target}{accessor_maker} ||= do { - require Method::Generate::Accessor; - Method::Generate::Accessor->new - })->generate_method($target, $name, \%spec); - push @{$INFO{$target}{attributes}||=[]}, $name, \%spec; + _install_tracked $target => has => sub { + my ($name_proto, %spec) = @_; + my $name_isref = ref $name_proto eq 'ARRAY'; + foreach my $name ($name_isref ? @$name_proto : $name_proto) { + my $spec_ref = $name_isref ? +{%spec} : \%spec; + ($INFO{$target}{accessor_maker} ||= do { + require Method::Generate::Accessor; + Method::Generate::Accessor->new + })->generate_method($target, $name, $spec_ref); + push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref; + $me->_maybe_reset_handlemoose($target); + } + }; + # install before/after/around subs + foreach my $type (qw(before after around)) { + _install_tracked $target => $type => sub { + require Class::Method::Modifiers; + push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ]; + $me->_maybe_reset_handlemoose($target); + }; + } + _install_tracked $target => requires => sub { + push @{$INFO{$target}{requires}||=[]}, @_; + $me->_maybe_reset_handlemoose($target); }; + _install_tracked $target => with => sub { + $me->apply_roles_to_package($target, @_); + $me->_maybe_reset_handlemoose($target); + }; + # grab all *non-constant* (stash slot is not a scalarref) subs present + # in the symbol table and store their refaddrs (no need to forcibly + # inflate constant subs into real subs) - also add '' to here (this + # is used later) with a map to the coderefs in case of copying or re-use + my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash); + @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods; + # a role does itself + $Role::Tiny::APPLIED_TO{$target} = { $target => undef }; + if ($INC{'Moo/HandleMoose.pm'}) { Moo::HandleMoose::inject_fake_metaclass_for($target); } - goto &Role::Tiny::import; +} + +sub unimport { + my $target = caller; + _unimport_coderefs($target, $INFO{$target}); +} + +sub _maybe_reset_handlemoose { + my ($class, $target) = @_; + if ($INC{"Moo/HandleMoose.pm"}) { + Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target); + } } sub _inhale_if_moose { my ($self, $role) = @_; _load_module($role); - if (!$INFO{$role} and $INC{"Moose.pm"}) { - if (my $meta = Class::MOP::class_of($role)) { - $INFO{$role}{methods} = { - map +($_ => $role->can($_)), $meta->get_method_list - }; - $Role::Tiny::APPLIED_TO{$role} = { - map +($_->name => 1), $meta->calculate_all_roles - }; - $INFO{$role}{requires} = [ $meta->get_required_method_list ]; - $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"}; - foreach my $method (keys %$map) { - foreach my $mod (@{$map->{$method}}) { - push @$mods, [ $type => $method => $mod ]; + my $meta; + if (!$INFO{$role} + and ( + $INC{"Moose.pm"} + and $meta = Class::MOP::class_of($role) + ) + or ( + $INC{"Mouse.pm"} + and $meta = Mouse::Util::find_meta($role) + ) + ) { + $INFO{$role}{methods} = { + map +($_ => $role->can($_)), + grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'), + $meta->get_method_list + }; + $Role::Tiny::APPLIED_TO{$role} = { + map +($_->name => 1), $meta->calculate_all_roles + }; + $INFO{$role}{requires} = [ $meta->get_required_method_list ]; + $INFO{$role}{attributes} = [ + map +($_ => do { + my $spec = { %{$meta->get_attribute($_)} }; + + if ($spec->{isa}) { + + my $get_constraint = do { + my $pkg = $meta->isa('Mouse::Meta::Role') + ? 'Mouse::Util::TypeConstraints' + : 'Moose::Util::TypeConstraints'; + _load_module($pkg); + $pkg->can('find_or_create_isa_type_constraint'); + }; + + my $tc = $get_constraint->($spec->{isa}); + my $check = $tc->_compiled_type_constraint; + + $spec->{isa} = sub { + &$check or die "Type constraint failed for $_[0]" + }; + + if ($spec->{coerce}) { + + # Mouse has _compiled_type_coercion straight on the TC object + $spec->{coerce} = $tc->${\( + $tc->can('coercion')||sub { $_[0] } + )}->_compiled_type_coercion; } } + $spec; + }), $meta->get_attribute_list + ]; + my $mods = $INFO{$role}{modifiers} = []; + foreach my $type (qw(before after around)) { + # Mouse pokes its own internals so we have to fall back to doing + # the same thing in the absence of the Moose API method + my $map = $meta->${\( + $meta->can("get_${type}_method_modifiers_map") + or sub { shift->{"${type}_method_modifiers"} } + )}; + foreach my $method (keys %$map) { + foreach my $mod (@{$map->{$method}}) { + push @$mods, [ $type => $method => $mod ]; + } } - require Class::Method::Modifiers if @$mods; - $INFO{$role}{inhaled_from_moose} = 1; } + require Class::Method::Modifiers if @$mods; + $INFO{$role}{inhaled_from_moose} = 1; } } @@ -64,7 +157,8 @@ sub _maybe_make_accessors { my ($self, $role, $target) = @_; my $m; if ($INFO{$role}{inhaled_from_moose} - or $m = Moo->_accessor_maker_for($target) + or $INC{"Moo.pm"} + and $m = Moo->_accessor_maker_for($target) and ref($m) ne 'Method::Generate::Accessor') { $self->_make_accessors($role, $target); } @@ -94,6 +188,14 @@ sub _make_accessors { } } +sub apply_roles_to_package { + my ($me, $to, @roles) = @_; + foreach my $role (@roles) { + $me->_inhale_if_moose($role); + } + $me->SUPER::apply_roles_to_package($to, @roles); +} + sub apply_single_role_to_package { my ($me, $to, $role) = @_; $me->_inhale_if_moose($role); @@ -111,10 +213,13 @@ sub create_class_with_roles { return $new_name if $Role::Tiny::COMPOSED{class}{$new_name}; - $me->_inhale_if_moose($_) for @roles; + foreach my $role (@roles) { + $me->_inhale_if_moose($role); + } my $m; - if ($m = Moo->_accessor_maker_for($superclass) + if ($INC{"Moo.pm"} + and $m = Moo->_accessor_maker_for($superclass) and ref($m) ne 'Method::Generate::Accessor') { # old fashioned way time. *{_getglob("${new_name}::ISA")} = [ $superclass ]; @@ -189,7 +294,7 @@ Moo::Role - Minimal Object Orientation support for Roles 1; -else where +And elsewhere: package Some::Class;