Commit | Line | Data |
d7d8d49b |
1 | package Mouse::Meta::Method::Delegation; |
3821b191 |
2 | use Mouse::Util qw(:meta); # enables strict and warnings |
3 | use Scalar::Util; |
d7d8d49b |
4 | |
5 | sub _generate_delegation{ |
21ee5bbb |
6 | my (undef, $attr, $handle_name, $method_to_call) = @_; |
7 | |
8 | my @curried_args; |
9 | if(ref($method_to_call) eq 'ARRAY'){ |
10 | ($method_to_call, @curried_args) = @{$method_to_call}; |
11 | } |
cb80a70a |
12 | |
1134f694 |
13 | # If it has a reader, we must use it to make method modifiers work |
14 | my $reader = $attr->get_read_method() || $attr->get_read_method_ref(); |
cb80a70a |
15 | |
73468dac |
16 | my $can_be_optimized = $attr->{_mouse_cache_method_delegation_can_be_optimized}; |
cb80a70a |
17 | |
18 | if(!defined $can_be_optimized){ |
1134f694 |
19 | my $tc = $attr->type_constraint; |
73468dac |
20 | $attr->{_mouse_cache_method_delegation_can_be_optimized} = |
cb80a70a |
21 | (defined($tc) && $tc->is_a_type_of('Object')) |
22 | && ($attr->is_required || $attr->has_default || $attr->has_builder) |
23 | && ($attr->is_lazy || !$attr->has_clearer); |
24 | } |
25 | |
26 | if($can_be_optimized){ |
27 | # need not check the attribute value |
28 | return sub { |
29 | return shift()->$reader()->$method_to_call(@curried_args, @_); |
30 | }; |
31 | } |
32 | else { |
33 | # need to check the attribute value |
34 | return sub { |
35 | my $instance = shift; |
36 | my $proxy = $instance->$reader(); |
37 | |
38 | my $error = !defined($proxy) ? ' is not defined' |
39 | : ref($proxy) && !Scalar::Util::blessed($proxy) ? qq{ is not an object (got '$proxy')} |
40 | : undef; |
41 | if ($error) { |
42 | $instance->meta->throw_error( |
43 | "Cannot delegate $handle_name to $method_to_call because " |
44 | . "the value of " |
45 | . $attr->name |
46 | . $error |
47 | ); |
48 | } |
49 | $proxy->$method_to_call(@curried_args, @_); |
50 | }; |
51 | } |
d7d8d49b |
52 | } |
53 | |
54 | |
55 | 1; |
56 | __END__ |
57 | |
58 | =head1 NAME |
59 | |
60 | Mouse::Meta::Method::Delegation - A Mouse method generator for delegation methods |
61 | |
62 | =head1 VERSION |
63 | |
5b3e7678 |
64 | This document describes Mouse version 0.89 |
d7d8d49b |
65 | |
66 | =head1 SEE ALSO |
67 | |
68 | L<Moose::Meta::Method::Delegation> |
69 | |
70 | =cut |