Refactor and optimize Mouse::Meta::Method::Accessor
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util; # enables strict and warnings
3 use Scalar::Util qw(blessed);
4
5 sub _generate_accessor{
6     my (undef, $attribute, $class, $type) = @_;
7
8     my $name          = $attribute->name;
9     my $default       = $attribute->default;
10     my $constraint    = $attribute->type_constraint;
11     my $builder       = $attribute->builder;
12     my $trigger       = $attribute->trigger;
13     my $is_weak       = $attribute->is_weak_ref;
14     my $should_deref  = $attribute->should_auto_deref;
15     my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
16
17     my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
18
19     my $self  = '$_[0]';
20     my $key   = "q{$name}";
21     my $slot  = "$self\->{$key}";
22
23     $type ||= 'accessor';
24
25     my $accessor = sprintf(qq{#line 1 "%s for %s (%s)"\n}, $type, $name, __FILE__)
26                  . "sub {\n";
27
28     if ($type eq 'accessor' || $type eq 'writer') {
29         if($type eq 'accessor'){
30             $accessor .= 
31                 'if (scalar(@_) >= 2) {' . "\n";
32         }
33         else{ # writer
34             $accessor .= 
35                 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
36                 '{' . "\n";
37         }
38                 
39         my $value = '$_[1]';
40
41         if (defined $constraint) {
42             if ($should_coerce) {
43                 $accessor .=
44                     "\n".
45                     'my $val = $constraint->coerce('.$value.');';
46                 $value = '$val';
47             }
48             $accessor .= 
49                 "\n".
50                 '$compiled_type_constraint->('.$value.') or
51                     $attribute->verify_type_constraint_error($name, '.$value.', $constraint);' . "\n";
52         }
53
54         # if there's nothing left to do for the attribute we can return during
55         # this setter
56         $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
57
58         $accessor .= "$slot = $value;\n";
59
60         if ($is_weak) {
61             $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
62         }
63
64         if ($trigger) {
65             $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
66         }
67
68         $accessor .= "}\n";
69     }
70     elsif($type eq 'reader') {
71         $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
72     }
73     else{
74         $class->throw_error("Unknown accessor type '$type'");
75     }
76
77     if ($attribute->is_lazy) {
78         my $value;
79
80         if (defined $builder){
81             $value = "$self->\$builder()";
82         }
83         elsif (ref($default) eq 'CODE'){
84             $value = "$self->\$default()";
85         }
86         else{
87             $value = '$default';
88         }
89
90         if($should_coerce){
91             $value = "\$constraint->coerce($value)";
92         }
93
94         $accessor .= "$slot = $value if !exists $slot;\n";
95     }
96
97     if ($should_deref) {
98         if ($constraint->is_a_type_of('ArrayRef')) {
99             $accessor .= "return \@{ $slot || [] } if wantarray;\n";
100         }
101         elsif($constraint->is_a_type_of('HashRef')){
102             $accessor .= "return \%{ $slot || {} } if wantarray;\n";
103         }
104         else{
105             $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
106         }
107     }
108
109     $accessor .= "return $slot;\n}\n";
110
111     #print $accessor, "\n";
112     my $code;
113     my $e = do{
114         local $@;
115         $code = eval $accessor;
116         $@;
117     };
118     die $e if $e;
119
120     return $code;
121 }
122
123 sub _generate_reader{
124     my $class = shift;
125     return $class->_generate_accessor(@_, 'reader');
126 }
127
128 sub _generate_writer{
129     my $class = shift;
130     return $class->_generate_accessor(@_, 'writer');
131 }
132
133
134 sub _generate_predicate {
135     my (undef, $attribute, $class) = @_;
136
137     my $slot = $attribute->name;
138     return sub{
139         return exists $_[0]->{$slot};
140     };
141 }
142
143 sub _generate_clearer {
144     my (undef, $attribute, $class) = @_;
145
146     my $slot = $attribute->name;
147
148    return sub{
149         delete $_[0]->{$slot};
150     };
151 }
152
153 sub _generate_delegation{
154     my (undef, $attribute, $class, $reader, $handle_name, $method_to_call) = @_;
155
156     return sub {
157         my $instance = shift;
158         my $proxy    = $instance->$reader();
159
160         my $error = !defined($proxy)                ? ' is not defined'
161                   : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
162                                                     : undef;
163         if ($error) {
164             $instance->meta->throw_error(
165                 "Cannot delegate $handle_name to $method_to_call because "
166                     . "the value of "
167                     . $attribute->name
168                     . $error
169              );
170         }
171         $proxy->$method_to_call(@_);
172     };
173 }
174
175
176 1;