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