| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mouse::Meta::Method::Delegation; | 
| 2 | 6 |  |  | 6 |  | 21 | use Mouse::Util qw(:meta); # enables strict and warnings | 
|  | 6 |  |  |  |  | 8 |  | 
|  | 6 |  |  |  |  | 38 |  | 
| 3 | 6 |  |  | 6 |  | 27 | use Scalar::Util; | 
|  | 6 |  |  |  |  | 7 |  | 
|  | 6 |  |  |  |  | 1909 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | sub _generate_delegation{ | 
| 6 | 22 |  |  | 22 |  | 36 | my (undef, $attr, $handle_name, $method_to_call) = @_; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 22 |  |  |  |  | 16 | my @curried_args; | 
| 9 | 22 | 100 |  |  |  | 45 | if(ref($method_to_call) eq 'ARRAY'){ | 
| 10 | 2 |  |  |  |  | 2 | ($method_to_call, @curried_args) = @{$method_to_call}; | 
|  | 2 |  |  |  |  | 4 |  | 
| 11 |  |  |  |  |  |  | } | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # If it has a reader, we must use it to make method modifiers work | 
| 14 | 22 |  | 66 |  |  | 49 | my $reader = $attr->get_read_method() || $attr->get_read_method_ref(); | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 22 |  |  |  |  | 29 | my $can_be_optimized = $attr->{_mouse_cache_method_delegation_can_be_optimized}; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 22 | 100 |  |  |  | 31 | if(!defined $can_be_optimized){ | 
| 19 | 14 |  |  |  |  | 23 | my $tc = $attr->type_constraint; | 
| 20 |  |  |  |  |  |  | $attr->{_mouse_cache_method_delegation_can_be_optimized} = | 
| 21 | 14 |  | 66 |  |  | 76 | (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 | 22 | 100 |  |  |  | 31 | if($can_be_optimized){ | 
| 27 |  |  |  |  |  |  | # need not check the attribute value | 
| 28 |  |  |  |  |  |  | return sub { | 
| 29 | 5 |  |  | 5 |  | 5975 | return shift()->$reader()->$method_to_call(@curried_args, @_); | 
|  |  |  |  | 1 |  |  |  | 
|  |  |  |  | 4 |  |  |  | 
|  |  |  |  | 3 |  |  |  | 
|  |  |  |  | 3 |  |  |  | 
| 30 | 5 |  |  |  |  | 56 | }; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  | else { | 
| 33 |  |  |  |  |  |  | # need to check the attribute value | 
| 34 |  |  |  |  |  |  | return sub { | 
| 35 | 16 |  |  | 16 |  | 4127 | my $instance = shift; | 
|  |  |  |  | 14 |  |  |  | 
|  |  |  |  | 16 |  |  |  | 
|  |  |  |  | 16 |  |  |  | 
|  |  |  |  | 14 |  |  |  | 
|  |  |  |  | 2 |  |  |  | 
|  |  |  |  | 2 |  |  |  | 
|  |  |  |  | 2 |  |  |  | 
| 36 | 16 |  |  |  |  | 47 | my $proxy    = $instance->$reader(); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 16 | 100 | 66 |  |  | 108 | my $error = !defined($proxy)                              ? ' is not defined' | 
|  |  | 100 |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | : ref($proxy) && !Scalar::Util::blessed($proxy) ? qq{ is not an object (got '$proxy')} | 
| 40 |  |  |  |  |  |  | : undef; | 
| 41 | 16 | 100 |  |  |  | 34 | if ($error) { | 
| 42 | 4 |  |  |  |  | 8 | $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 | 12 |  |  |  |  | 41 | $proxy->$method_to_call(@curried_args, @_); | 
| 50 | 17 |  |  |  |  | 180 | }; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | 1; | 
| 56 |  |  |  |  |  |  | __END__ |