2 package Moose::Meta::Method::Delegation;
8 use Scalar::Util 'blessed', 'weaken';
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::Method',
13 'Class::MOP::Method::Generated';
20 ( exists $options{attribute} )
21 || confess "You must supply an attribute to construct with";
23 ( blessed( $options{attribute} )
24 && $options{attribute}->isa('Moose::Meta::Attribute') )
26 "You must supply an attribute which is a 'Moose::Meta::Attribute' instance";
28 ( $options{package_name} && $options{name} )
30 "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
32 ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} )
33 || ( 'CODE' eq ref $options{delegate_to_method} ) )
35 'You must supply a delegate_to_method which is a method name or a CODE reference';
37 exists $options{curried_arguments}
38 || ( $options{curried_arguments} = [] );
40 ( $options{curried_arguments} &&
41 ( 'ARRAY' eq ref $options{curried_arguments} ) )
42 || confess 'You must supply a curried_arguments which is an ARRAY reference';
44 my $self = $class->_new( \%options );
46 weaken( $self->{'attribute'} );
48 $self->_initialize_body;
55 my $options = @_ == 1 ? $_[0] : {@_};
57 return bless $options, $class;
60 sub curried_arguments { (shift)->{'curried_arguments'} }
62 sub associated_attribute { (shift)->{'attribute'} }
64 sub delegate_to_method { (shift)->{'delegate_to_method'} }
66 sub _initialize_body {
69 my $method_to_call = $self->delegate_to_method;
70 return $self->{body} = $method_to_call
71 if ref $method_to_call;
73 my $accessor = $self->_get_delegate_accessor;
75 my $handle_name = $self->name;
77 # NOTE: we used to do a goto here, but the goto didn't handle
78 # failure correctly (it just returned nothing), so I took that
79 # out. However, the more I thought about it, the less I liked it
80 # doing the goto, and I preferred the act of delegation being
81 # actually represented in the stack trace. - SL
82 # not inlining this, since it won't really speed things up at
83 # all... the only thing that would end up different would be
84 # interpolating in $method_to_call, and a bunch of things in the
85 # error handling that mostly never gets called - doy
88 my $proxy = $instance->$accessor();
91 = !defined $proxy ? ' is not defined'
92 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
97 "Cannot delegate $handle_name to $method_to_call because "
99 . $self->associated_attribute->name
101 method_name => $method_to_call,
105 unshift @_, @{ $self->curried_arguments };
106 $proxy->$method_to_call(@_);
110 sub _get_delegate_accessor {
112 my $attr = $self->associated_attribute;
115 # always use a named method when
116 # possible, if you use the method
117 # ref and there are modifiers on
118 # the accessors then it will not
119 # pick up the modifiers too. Only
120 # the named method will assure that
121 # we also have any modifiers run.
123 my $accessor = $attr->has_read_method
124 ? $attr->get_read_method
125 : $attr->get_read_method_ref;
127 $accessor = $accessor->body if Scalar::Util::blessed $accessor;
134 # ABSTRACT: A Moose Method metaclass for delegation methods
142 This is a subclass of L<Moose::Meta::Method> for delegation
149 =item B<< Moose::Meta::Method::Delegation->new(%options) >>
151 This creates the delegation methods based on the provided C<%options>.
157 This must be an instance of C<Moose::Meta::Attribute> which this
158 accessor is being generated for. This options is B<required>.
160 =item I<delegate_to_method>
162 The method in the associated attribute's value to which we
163 delegate. This can be either a method name or a code reference.
165 =item I<curried_arguments>
167 An array reference of arguments that will be prepended to the argument list for
168 any call to the delegating method.
172 =item B<< $metamethod->associated_attribute >>
174 Returns the attribute associated with this method.
176 =item B<< $metamethod->curried_arguments >>
178 Return any curried arguments that will be passed to the delegated method.
180 =item B<< $metamethod->delegate_to_method >>
182 Returns the method to which this method delegates, as passed to the
189 See L<Moose/BUGS> for details on reporting bugs.