X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FRole.pm;h=fe6575ec3583d923d984d93d168faad4d5b83ecd;hb=823419c540f9e77090f31f11e04b14477c0372c4;hp=07203c376c51c7e394e4aede792a8b2433c8d940;hpb=d99db7b6a63d3377b7854c2529a299584499080b;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 07203c3..fe6575e 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -1,86 +1,155 @@ -#!/usr/bin/env perl package Mouse::Meta::Role; -use strict; -use warnings; - -do { - my %METACLASS_CACHE; - - # because Mouse doesn't introspect existing classes, we're forced to - # only pay attention to other Mouse classes - sub _metaclass_cache { - my $class = shift; - my $name = shift; - return $METACLASS_CACHE{$name}; - } +use Mouse::Util qw(:meta); # enables strict and warnings - sub initialize { - my $class = shift; - my $name = shift; - $METACLASS_CACHE{$name} = $class->new(name => $name) - if !exists($METACLASS_CACHE{$name}); - return $METACLASS_CACHE{$name}; - } -}; +use Mouse::Meta::Module; +our @ISA = qw(Mouse::Meta::Module); + +sub method_metaclass; -sub new { +sub _construct_meta { my $class = shift; + my %args = @_; - $args{attributes} ||= {}; + $args{methods} = {}; + $args{attributes} = {}; + $args{required_methods} = []; + $args{roles} = []; + + my $self = bless \%args, ref($class) || $class; + if($class ne __PACKAGE__){ + $self->meta->_initialize_object($self, \%args); + } - bless \%args, $class; + return $self; } -sub name { $_[0]->{name} } +sub create_anon_role{ + my $self = shift; + return $self->create(undef, @_); +} + +sub is_anon_role; + +sub get_roles; + +sub calculate_all_roles { + my $self = shift; + my %seen; + return grep { !$seen{ $_->name }++ } + ($self, map { $_->calculate_all_roles } @{ $self->get_roles }); +} + +sub get_required_method_list{ + return @{ $_[0]->{required_methods} }; +} + +sub add_required_methods { + my($self, @methods) = @_; + my %required = map{ $_ => 1 } @{$self->{required_methods}}; + push @{$self->{required_methods}}, grep{ !$required{$_}++ && !$self->has_method($_) } @methods; + return; +} + +sub requires_method { + my($self, $name) = @_; + return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0; +} sub add_attribute { my $self = shift; my $name = shift; - my $spec = shift; - $self->{attributes}->{$name} = $spec; + + $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ }; + return; } -sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } -sub get_attribute_list { keys %{ $_[0]->{attributes} } } -sub get_attribute { $_[0]->{attributes}->{$_[1]} } +# Moose uses Application::ToInstance, Application::ToClass, Application::ToRole sub apply { - my $self = shift; - my $class = shift; + my $self = shift; + my $consumer = shift; - for my $name ($self->get_attribute_list) { - next if $class->has_attribute($name); - my $spec = $self->get_attribute($name); - Mouse::Meta::Attribute->create($class, $name, %$spec); + require 'Mouse/Meta/Role/Application.pm'; + return Mouse::Meta::Role::Application->new(@_)->apply($self, $consumer); +} + + +sub combine { + my($self, @role_specs) = @_; + + require 'Mouse/Meta/Role/Composite.pm'; + my $composite = Mouse::Meta::Role::Composite->create_anon_role(); + + foreach my $role_spec (@role_specs) { + my($role_name, $args) = @{$role_spec}; + $role_name->meta->apply($composite, %{$args}); } + return $composite; +} - for my $modifier_type (qw/before after around/) { - my $add_method = "add_${modifier_type}_method_modifier"; - my $modified = $self->{"${modifier_type}_method_modifiers"}; +sub add_before_method_modifier; +sub add_around_method_modifier; +sub add_after_method_modifier; - for my $method_name (keys %$modified) { - for my $code (@{ $modified->{$method_name} }) { - $class->$add_method($method_name => $code); - } - } +sub get_before_method_modifiers; +sub get_around_method_modifiers; +sub get_after_method_modifiers; + +sub add_override_method_modifier{ + my($self, $method_name, $method) = @_; + + if($self->has_method($method_name)){ + # This error happens in the override keyword or during role composition, + # so I added a message, "A local method of ...", only for compatibility (gfx) + $self->throw_error("Cannot add an override of method '$method_name' " + . "because there is a local version of '$method_name'" + . "(A local method of the same name as been found)"); } + + $self->{override_method_modifiers}->{$method_name} = $method; +} + +sub get_override_method_modifier { + my ($self, $method_name) = @_; + return $self->{override_method_modifiers}->{$method_name}; } -for my $modifier_type (qw/before after around/) { - no strict 'refs'; - *{ __PACKAGE__ . '::' . "add_${modifier_type}_method_modifier" } = sub { - my ($self, $method_name, $method) = @_; +sub does_role { + my ($self, $role_name) = @_; - push @{ $self->{"${modifier_type}_method_modifiers"}->{$method_name} }, - $method; - }; + (defined $role_name) + || $self->throw_error("You must supply a role name to look for"); - *{ __PACKAGE__ . '::' . "get_${modifier_type}_method_modifiers" } = sub { - my ($self, $method_name, $method) = @_; - @{ $self->{"${modifier_type}_method_modifiers"}->{$method_name} || [] } - }; + $role_name = $role_name->name if ref $role_name; + + # if we are it,.. then return true + return 1 if $role_name eq $self->name; + # otherwise.. check our children + for my $role (@{ $self->get_roles }) { + return 1 if $role->does_role($role_name); + } + return 0; } 1; +__END__ + +=head1 NAME + +Mouse::Meta::Role - The Mouse Role metaclass + +=head1 VERSION + +This document describes Mouse version 0.70 + +=head1 DESCRIPTION + +This class is a meta object protocol for Mouse roles, +which is a subset of Moose::Meta:::Role. + +=head1 SEE ALSO + +L +=cut