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