Merge branch 'master' into attribute_helpers
[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
b9e554fa 10our $VERSION = '0.89';
a05f85c1 11$VERSION = eval $VERSION;
12our $AUTHORITY = 'cpan:STEVAN';
13
01164604 14use base 'Moose::Meta::Method',
15 'Class::MOP::Method::Generated';
a05f85c1 16
17
18sub new {
19 my $class = shift;
20 my %options = @_;
21
46f7e6a5 22 ( exists $options{attribute} )
a05f85c1 23 || confess "You must supply an attribute to construct with";
24
46f7e6a5 25 ( blessed( $options{attribute} )
26 && $options{attribute}->isa('Moose::Meta::Attribute') )
27 || confess
28 "You must supply an attribute which is a 'Moose::Meta::Attribute' instance";
a05f85c1 29
46f7e6a5 30 ( $options{package_name} && $options{name} )
31 || confess
32 "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
a05f85c1 33
46f7e6a5 34 ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} )
35 || ( 'CODE' eq ref $options{delegate_to_method} ) )
36 || confess
37 'You must supply a delegate_to_method which is a method name or a CODE reference';
a05f85c1 38
4adb0e7b 39 exists $options{curried_arguments}
40 || ( $options{curried_arguments} = [] );
2de18801 41
4adb0e7b 42 ( $options{curried_arguments} &&
43 ( 'ARRAY' eq ref $options{curried_arguments} ) )
44 || confess 'You must supply a curried_arguments which is an ARRAY reference';
2de18801 45
46f7e6a5 46 my $self = $class->_new( \%options );
47
48 weaken( $self->{'attribute'} );
49
50 $self->_initialize_body;
a05f85c1 51
52 return $self;
53}
54
01cd78f8 55sub _new {
56 my $class = shift;
57 my $options = @_ == 1 ? $_[0] : {@_};
58
59 return bless $options, $class;
60}
61
2de18801 62sub curried_arguments { (shift)->{'curried_arguments'} }
63
01cd78f8 64sub associated_attribute { (shift)->{'attribute'} }
65
46f7e6a5 66sub delegate_to_method { (shift)->{'delegate_to_method'} }
67
68sub _initialize_body {
69 my $self = shift;
70
71 my $method_to_call = $self->delegate_to_method;
72 return $self->{body} = $method_to_call
73 if ref $method_to_call;
74
75 my $accessor = $self->_get_delegate_accessor;
76
77 my $handle_name = $self->name;
78
79 # NOTE: we used to do a goto here, but the goto didn't handle
80 # failure correctly (it just returned nothing), so I took that
81 # out. However, the more I thought about it, the less I liked it
16f00af3 82 # doing the goto, and I preferred the act of delegation being
46f7e6a5 83 # actually represented in the stack trace. - SL
5120f8ff 84 # not inlining this, since it won't really speed things up at
85 # all... the only thing that would end up different would be
86 # interpolating in $method_to_call, and a bunch of things in the
87 # error handling that mostly never gets called - doy
46f7e6a5 88 $self->{body} = sub {
89 my $instance = shift;
90 my $proxy = $instance->$accessor();
6148c167 91
92 my $error
2cc6f982 93 = !defined $proxy ? ' is not defined'
94 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
95 : undef;
6148c167 96
97 if ($error) {
98 $self->throw_error(
99 "Cannot delegate $handle_name to $method_to_call because "
100 . "the value of "
101 . $self->associated_attribute->name
102 . $error,
103 method_name => $method_to_call,
104 object => $instance
46f7e6a5 105 );
6148c167 106 }
2de18801 107 my @args = (@{ $self->curried_arguments }, @_);
108 $proxy->$method_to_call(@args);
46f7e6a5 109 };
110}
111
112sub _get_delegate_accessor {
113 my $self = shift;
114
115 my $accessor = $self->associated_attribute->get_read_method_ref;
116
117 $accessor = $accessor->body if blessed $accessor;
118
119 return $accessor;
120}
121
a05f85c1 1221;
01cd78f8 123
124__END__
125
126=pod
127
128=head1 NAME
129
130Moose::Meta::Method::Delegation - A Moose Method metaclass for delegation methods
131
132=head1 DESCRIPTION
133
134This is a subclass of L<Moose::Meta::Method> for delegation
135methods.
136
137=head1 METHODS
138
139=over 4
140
e6fb6ad2 141=item B<< Moose::Meta::Method::Delegation->new(%options) >>
01cd78f8 142
e6fb6ad2 143This creates the delegation methods based on the provided C<%options>.
01cd78f8 144
145=over 4
146
147=item I<attribute>
148
149This must be an instance of C<Moose::Meta::Attribute> which this
e6fb6ad2 150accessor is being generated for. This options is B<required>.
01cd78f8 151
46f7e6a5 152=item I<delegate_to_method>
153
154The method in the associated attribute's value to which we
155delegate. This can be either a method name or a code reference.
156
2de18801 157=item I<curried_arguments>
158
159An array reference of arguments that will be prepended to the argument list for
160any call to the delegating method.
161
01cd78f8 162=back
163
e6fb6ad2 164=item B<< $metamethod->associated_attribute >>
01cd78f8 165
166Returns the attribute associated with this method.
167
2de18801 168=item B<< $metamethod->curried_arguments >>
169
170Return any curried arguments that will be passed to the delegated method.
171
e6fb6ad2 172=item B<< $metamethod->delegate_to_method >>
46f7e6a5 173
e6fb6ad2 174Returns the method to which this method delegates, as passed to the
175constructor.
46f7e6a5 176
01cd78f8 177=back
178
179=head1 BUGS
180
d03bd989 181All complex software has bugs lurking in it, and this module is no
01cd78f8 182exception. If you find a bug please either email me, or add the bug
183to cpan-RT.
184
185=head1 AUTHOR
186
187Dave Rolsky E<lt>autarch@urth.orgE<gt>
188
189=head1 COPYRIGHT AND LICENSE
190
2840a3b2 191Copyright 2009 by Infinity Interactive, Inc.
01cd78f8 192
193L<http://www.iinteractive.com>
194
195This library is free software; you can redistribute it and/or modify
196it under the same terms as Perl itself.
197
198=cut