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