X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FRole%2FComposite.pm;h=c7329d47326c313de43f55e30a94cdf66566a154;hb=7071e2cbb253e6c9ede2055d1309614590616c7f;hp=6e15b3145397065713bc3fe1336e44a8e796fea8;hpb=fb1e11d526a7d3608132ba484525980e9fafcc4f;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Role/Composite.pm b/lib/Moose/Meta/Role/Composite.pm index 6e15b31..c7329d4 100644 --- a/lib/Moose/Meta/Role/Composite.pm +++ b/lib/Moose/Meta/Role/Composite.pm @@ -4,61 +4,91 @@ use strict; use warnings; use metaclass; -use Carp 'confess'; -use Scalar::Util 'blessed', 'reftype'; +use Scalar::Util 'blessed'; -use Data::Dumper; - -our $VERSION = '0.01'; +our $VERSION = '0.89_02'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use base 'Moose::Meta::Role'; # NOTE: -# we need to override the ->name +# we need to override the ->name # method from Class::MOP::Package -# since we don't have an actual +# since we don't have an actual # package for this. # - SL __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); # NOTE: -# Again, since we don't have a real -# package to store our methods in, -# we use a HASH ref instead. +# Again, since we don't have a real +# package to store our methods in, +# we use a HASH ref instead. # - SL __PACKAGE__->meta->add_attribute('methods' => ( reader => 'get_method_map', default => sub { {} } )); +__PACKAGE__->meta->add_attribute( + 'application_role_summation_class', + reader => 'application_role_summation_class', + default => 'Moose::Meta::Role::Application::RoleSummation', +); + sub new { my ($class, %params) = @_; # the roles param is required ... - ($_->isa('Moose::Meta::Role')) - || confess "The list of roles must be instances of Moose::Meta::Role, not $_" - foreach @{$params{roles}}; + foreach ( @{$params{roles}} ) { + unless ( $_->isa('Moose::Meta::Role') ) { + require Moose; + Moose->throw_error("The list of roles must be instances of Moose::Meta::Role, not $_"); + } + } # and the name is created from the # roles if one has not been provided $params{name} ||= (join "|" => map { $_->name } @{$params{roles}}); - $class->meta->new_object(%params); + $class->_new(\%params); } -# NOTE: -# we need to override this cause -# we dont have that package I was -# talking about above. -# - SL -sub alias_method { +# This is largely a cope of what's in Moose::Meta::Role (itself +# largely a copy of Class::MOP::Class). However, we can't actually +# call add_package_symbol, because there's no package to which which +# add the symbol. +sub add_method { my ($self, $method_name, $method) = @_; - (defined $method_name && $method_name) - || confess "You must define a method name"; - my $body = (blessed($method) ? $method->body : $method); - ('CODE' eq (reftype($body) || '')) - || confess "Your code block must be a CODE reference"; + unless ( defined $method_name && $method_name ) { + Moose->throw_error("You must define a method name"); + } + + my $body; + if (blessed($method)) { + $body = $method->body; + if ($method->package_name ne $self->name) { + $method = $method->clone( + package_name => $self->name, + name => $method_name + ) if $method->can('clone'); + } + } + else { + $body = $method; + $method = $self->wrap_method_body( body => $body, name => $method_name ); + } + + $self->get_method_map->{$method_name} = $method; +} - $self->get_method_map->{$method_name} = $body; +sub apply_params { + my ($self, $role_params) = @_; + Class::MOP::load_class($self->application_role_summation_class); + + $self->application_role_summation_class->new( + role_params => $role_params, + )->apply($self); + + return $self; } 1; @@ -73,19 +103,43 @@ Moose::Meta::Role::Composite - An object to represent the set of roles =head1 DESCRIPTION +A composite is a role that consists of a set of two or more roles. + +The API of a composite role is almost identical to that of a regular +role. + +=head1 INHERITANCE + +C is a subclass of L. + =head2 METHODS =over 4 -=item B +=item B<< Moose::Meta::Role::Composite->new(%options) >> + +This returns a new composite role object. It accepts the same +options as its parent class, with a few changes: + +=over 8 -=item B +=item * roles -=item B +This option is an array reference containing a list of +L object. This is a required option. -=item B +=item * name -=item B +If a name is not given, one is generated from the roles provided. + +=item * apply_params(\%role_params) + +Creates a new RoleSummation role application with C<%role_params> and applies +the composite role to it. The RoleSummation role application class used is +determined by the composite role's C +attribute. + +=back =back @@ -101,11 +155,11 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006, 2007 by Infinity Interactive, Inc. +Copyright 2006-2009 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut