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