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