2 package Moose::Meta::Method::Delegation;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.57';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::Method';
21 ( exists $options{attribute} )
22 || confess "You must supply an attribute to construct with";
24 ( blessed( $options{attribute} )
25 && $options{attribute}->isa('Moose::Meta::Attribute') )
27 "You must supply an attribute which is a 'Moose::Meta::Attribute' instance";
29 ( $options{package_name} && $options{name} )
31 "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
33 ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} )
34 || ( 'CODE' eq ref $options{delegate_to_method} ) )
36 'You must supply a delegate_to_method which is a method name or a CODE reference';
38 my $self = $class->_new( \%options );
40 weaken( $self->{'attribute'} );
42 $self->_initialize_body;
49 my $options = @_ == 1 ? $_[0] : {@_};
51 return bless $options, $class;
54 sub associated_attribute { (shift)->{'attribute'} }
56 sub delegate_to_method { (shift)->{'delegate_to_method'} }
58 sub _initialize_body {
61 my $method_to_call = $self->delegate_to_method;
62 return $self->{body} = $method_to_call
63 if ref $method_to_call;
65 my $accessor = $self->_get_delegate_accessor;
67 my $handle_name = $self->name;
69 # NOTE: we used to do a goto here, but the goto didn't handle
70 # failure correctly (it just returned nothing), so I took that
71 # out. However, the more I thought about it, the less I liked it
72 # doing the goto, and I prefered the act of delegation being
73 # actually represented in the stack trace. - SL
76 my $proxy = $instance->$accessor();
78 || $self->throw_error(
79 "Cannot delegate $handle_name to $method_to_call because "
83 method_name => $method_to_call,
86 $proxy->$method_to_call(@_);
90 sub _get_delegate_accessor {
93 my $accessor = $self->associated_attribute->get_read_method_ref;
95 $accessor = $accessor->body if blessed $accessor;
108 Moose::Meta::Method::Delegation - A Moose Method metaclass for delegation methods
112 This is a subclass of L<Moose::Meta::Method> for delegation
119 =item B<new (%options)>
121 This creates the method based on the criteria in C<%options>,
128 This must be an instance of C<Moose::Meta::Attribute> which this
129 accessor is being generated for. This paramter is B<required>.
131 =item I<delegate_to_method>
133 The method in the associated attribute's value to which we
134 delegate. This can be either a method name or a code reference.
138 =item B<associated_attribute>
140 Returns the attribute associated with this method.
142 =item B<delegate_to_method>
144 Returns the method to which this method delegates.
150 All complex software has bugs lurking in it, and this module is no
151 exception. If you find a bug please either email me, or add the bug
156 Dave Rolsky E<lt>autarch@urth.orgE<gt>
158 =head1 COPYRIGHT AND LICENSE
160 Copyright 2008 by Infinity Interactive, Inc.
162 L<http://www.iinteractive.com>
164 This library is free software; you can redistribute it and/or modify
165 it under the same terms as Perl itself.