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