X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoo%2FRole.pm;h=21b304af78c20ab34cc7fc7bb6edc1cdbad11b3b;hb=e18659958c8a6bf3fc60b9531e40acbea275076f;hp=564341334985e6ec9ab5bec1e525a82128f7f69f;hpb=1e84d6ace7bd17976c803aa81df5390ed9a00bb0;p=gitmo%2FMoo.git diff --git a/lib/Moo/Role.pm b/lib/Moo/Role.pm index 5643413..21b304a 100644 --- a/lib/Moo/Role.pm +++ b/lib/Moo/Role.pm @@ -5,6 +5,9 @@ use Moo::_Utils; use Role::Tiny (); use base qw(Role::Tiny); +our $VERSION = '1.003001'; +$VERSION = eval $VERSION; + require Moo::sification; BEGIN { *INFO = \%Role::Tiny::INFO } @@ -27,12 +30,18 @@ sub import { } $INFO{$target} ||= {}; # get symbol table reference - my $stash = do { no strict 'refs'; \%{"${target}::"} }; + my $stash = _getstash($target); _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; + my $name_proto = shift; + my @name_proto = ref $name_proto eq 'ARRAY' ? @$name_proto : $name_proto; + if (@_ % 2 != 0) { + require Carp; + Carp::croak("Invalid options for " . join(', ', map "'$_'", @name_proto) + . " attribute(s): even number of arguments expected, got " . scalar @_) + } + my %spec = @_; + foreach my $name (@name_proto) { + my $spec_ref = @name_proto > 1 ? +{%spec} : \%spec; ($INFO{$target}{accessor_maker} ||= do { require Method::Generate::Accessor; Method::Generate::Accessor->new @@ -168,29 +177,30 @@ sub _inhale_if_moose { } require Class::Method::Modifiers if @$mods; $INFO{$role}{inhaled_from_moose} = 1; + $INFO{$role}{is_role} = 1; } } sub _maybe_make_accessors { - my ($self, $role, $target) = @_; + my ($self, $target, $role) = @_; my $m; - if ($INFO{$role}{inhaled_from_moose} + if ($INFO{$role} && $INFO{$role}{inhaled_from_moose} or $INC{"Moo.pm"} and $m = Moo->_accessor_maker_for($target) and ref($m) ne 'Method::Generate::Accessor') { - $self->_make_accessors($role, $target); + $self->_make_accessors($target, $role); } } sub _make_accessors_if_moose { - my ($self, $role, $target) = @_; - if ($INFO{$role}{inhaled_from_moose}) { - $self->_make_accessors($role, $target); + my ($self, $target, $role) = @_; + if ($INFO{$role} && $INFO{$role}{inhaled_from_moose}) { + $self->_make_accessors($target, $role); } } sub _make_accessors { - my ($self, $role, $target) = @_; + my ($self, $target, $role) = @_; my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do { require Method::Generate::Accessor; Method::Generate::Accessor->new @@ -206,29 +216,31 @@ sub _make_accessors { } } +sub role_application_steps { + qw(_handle_constructor _maybe_make_accessors), + $_[0]->SUPER::role_application_steps; +} + sub apply_roles_to_package { my ($me, $to, @roles) = @_; foreach my $role (@roles) { - $me->_inhale_if_moose($role); + $me->_inhale_if_moose($role); + die "${role} is not a Moo::Role" unless $INFO{$role}; } $me->SUPER::apply_roles_to_package($to, @roles); } sub apply_single_role_to_package { my ($me, $to, $role) = @_; - die "${role} is not a Moo::Role" unless my $info = $INFO{$role}; $me->_inhale_if_moose($role); - $me->_handle_constructor($to, $INFO{$role}{attributes}); - $me->_maybe_make_accessors($role, $to); + die "${role} is not a Moo::Role" unless $INFO{$role}; $me->SUPER::apply_single_role_to_package($to, $role); } sub create_class_with_roles { my ($me, $superclass, @roles) = @_; - my $new_name = join( - '__WITH__', $superclass, my $compose_name = join '__AND__', @roles - ); + my ($new_name, $compose_name) = $me->_composite_name($superclass, @roles); return $new_name if $Role::Tiny::COMPOSED{class}{$new_name}; @@ -251,14 +263,12 @@ sub create_class_with_roles { $me->SUPER::create_class_with_roles($superclass, @roles); foreach my $role (@roles) { - die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; + die "${role} is not a Role::Tiny" unless $INFO{$role}; } - $Moo::MAKERS{$new_name} = {}; + $Moo::MAKERS{$new_name} = {is_class => 1}; - $me->_handle_constructor( - $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ] - ); + $me->_handle_constructor($new_name, $_) for @roles; return $new_name; } @@ -307,7 +317,7 @@ sub _composable_package_for { my ($self, $role) = @_; my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role; return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name}; - $self->_make_accessors_if_moose($role, $composed_name); + $self->_make_accessors_if_moose($composed_name, $role); $self->SUPER::_composable_package_for($role); } @@ -317,7 +327,8 @@ sub _install_single_modifier { } sub _handle_constructor { - my ($me, $to, $attr_info) = @_; + my ($me, $to, $role) = @_; + my $attr_info = $INFO{$role} && $INFO{$role}{attributes}; return unless $attr_info && @$attr_info; if ($INFO{$to}) { push @{$INFO{$to}{attributes}||=[]}, @$attr_info;