meta_lookup needs to propagate downwards, if unspecified
[gitmo/Moose.git] / lib / Moose / Meta / Method / Delegation.pm
CommitLineData
a05f85c1 1
2package Moose::Meta::Method::Delegation;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken';
9
01164604 10use base 'Moose::Meta::Method',
11 'Class::MOP::Method::Generated';
a05f85c1 12
13
14sub new {
15 my $class = shift;
16 my %options = @_;
17
46f7e6a5 18 ( exists $options{attribute} )
a05f85c1 19 || confess "You must supply an attribute to construct with";
20
46f7e6a5 21 ( blessed( $options{attribute} )
22 && $options{attribute}->isa('Moose::Meta::Attribute') )
23 || confess
24 "You must supply an attribute which is a 'Moose::Meta::Attribute' instance";
a05f85c1 25
46f7e6a5 26 ( $options{package_name} && $options{name} )
27 || confess
28 "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
a05f85c1 29
46f7e6a5 30 ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} )
31 || ( 'CODE' eq ref $options{delegate_to_method} ) )
32 || confess
33 'You must supply a delegate_to_method which is a method name or a CODE reference';
a05f85c1 34
4adb0e7b 35 exists $options{curried_arguments}
36 || ( $options{curried_arguments} = [] );
2de18801 37
4adb0e7b 38 ( $options{curried_arguments} &&
39 ( 'ARRAY' eq ref $options{curried_arguments} ) )
40 || confess 'You must supply a curried_arguments which is an ARRAY reference';
2de18801 41
46f7e6a5 42 my $self = $class->_new( \%options );
43
44 weaken( $self->{'attribute'} );
45
46 $self->_initialize_body;
a05f85c1 47
48 return $self;
49}
50
01cd78f8 51sub _new {
52 my $class = shift;
53 my $options = @_ == 1 ? $_[0] : {@_};
54
55 return bless $options, $class;
56}
57
2de18801 58sub curried_arguments { (shift)->{'curried_arguments'} }
59
01cd78f8 60sub associated_attribute { (shift)->{'attribute'} }
61
46f7e6a5 62sub delegate_to_method { (shift)->{'delegate_to_method'} }
63
64sub _initialize_body {
65 my $self = shift;
66
67 my $method_to_call = $self->delegate_to_method;
68 return $self->{body} = $method_to_call
69 if ref $method_to_call;
70
71 my $accessor = $self->_get_delegate_accessor;
72
73 my $handle_name = $self->name;
74
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
16f00af3 78 # doing the goto, and I preferred the act of delegation being
46f7e6a5 79 # actually represented in the stack trace. - SL
5120f8ff 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
46f7e6a5 84 $self->{body} = sub {
85 my $instance = shift;
86 my $proxy = $instance->$accessor();
6148c167 87
88 my $error
2cc6f982 89 = !defined $proxy ? ' is not defined'
90 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
91 : undef;
6148c167 92
93 if ($error) {
94 $self->throw_error(
95 "Cannot delegate $handle_name to $method_to_call because "
96 . "the value of "
97 . $self->associated_attribute->name
98 . $error,
99 method_name => $method_to_call,
100 object => $instance
46f7e6a5 101 );
6148c167 102 }
c279b82f 103 unshift @_, @{ $self->curried_arguments };
104 $proxy->$method_to_call(@_);
46f7e6a5 105 };
106}
107
108sub _get_delegate_accessor {
109 my $self = shift;
53b3e214 110 my $attr = $self->associated_attribute;
111
112 # NOTE:
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.
120 # - SL
121 my $accessor = $attr->has_read_method
122 ? $attr->get_read_method
123 : $attr->get_read_method_ref;
124
125 $accessor = $accessor->body if Scalar::Util::blessed $accessor;
46f7e6a5 126
127 return $accessor;
128}
129
a05f85c1 1301;
01cd78f8 131
ad46f524 132# ABSTRACT: A Moose Method metaclass for delegation methods
133
01cd78f8 134__END__
135
136=pod
137
01cd78f8 138=head1 DESCRIPTION
139
140This is a subclass of L<Moose::Meta::Method> for delegation
141methods.
142
143=head1 METHODS
144
145=over 4
146
e6fb6ad2 147=item B<< Moose::Meta::Method::Delegation->new(%options) >>
01cd78f8 148
e6fb6ad2 149This creates the delegation methods based on the provided C<%options>.
01cd78f8 150
151=over 4
152
153=item I<attribute>
154
155This must be an instance of C<Moose::Meta::Attribute> which this
e6fb6ad2 156accessor is being generated for. This options is B<required>.
01cd78f8 157
46f7e6a5 158=item I<delegate_to_method>
159
160The method in the associated attribute's value to which we
161delegate. This can be either a method name or a code reference.
162
2de18801 163=item I<curried_arguments>
164
165An array reference of arguments that will be prepended to the argument list for
166any call to the delegating method.
167
01cd78f8 168=back
169
e6fb6ad2 170=item B<< $metamethod->associated_attribute >>
01cd78f8 171
172Returns the attribute associated with this method.
173
2de18801 174=item B<< $metamethod->curried_arguments >>
175
176Return any curried arguments that will be passed to the delegated method.
177
e6fb6ad2 178=item B<< $metamethod->delegate_to_method >>
46f7e6a5 179
e6fb6ad2 180Returns the method to which this method delegates, as passed to the
181constructor.
46f7e6a5 182
01cd78f8 183=back
184
185=head1 BUGS
186
d4048ef3 187See L<Moose/BUGS> for details on reporting bugs.
01cd78f8 188
01cd78f8 189=cut