Checking in changes prior to tagging of version 0.50_04. Changelog diff is:
[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 use warnings FATAL => 'recursion';
4
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") if scalar(@_) >= 2;' . "\n";
75     }
76     else{
77         $class->throw_error("Unknown accessor type '$type'");
78     }
79
80     if ($attribute->is_lazy) {
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 .= "if(!exists $slot){\n";
94         if($should_coerce){
95             $accessor .= "$slot = \$constraint->coerce($value)";
96         }
97         elsif(defined $constraint){
98             $accessor .= "my \$tmp = $value;\n";
99
100             $accessor .= "\$compiled_type_constraint->(\$tmp)";
101             $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
102             $accessor .= "$slot = \$tmp;\n";
103         }
104         else{
105             $accessor .= "$slot = $value;\n";
106         }
107         if ($is_weak) {
108             $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\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 $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_accessor{
140     my $class = shift;
141     return $class->_generate_accessor_any(rw => @_);
142 }
143
144 sub _generate_reader {
145     my $class = shift;
146     return $class->_generate_accessor_any(ro => @_);
147 }
148
149 sub _generate_writer {
150     my $class = shift;
151     return $class->_generate_accessor_any(wo => @_);
152 }
153
154 sub _generate_predicate {
155     my (undef, $attribute, $class) = @_;
156
157     my $slot = $attribute->name;
158     return sub{
159         return exists $_[0]->{$slot};
160     };
161 }
162
163 sub _generate_clearer {
164     my (undef, $attribute, $class) = @_;
165
166     my $slot = $attribute->name;
167     return sub{
168         delete $_[0]->{$slot};
169     };
170 }
171
172 1;
173 __END__
174
175 =head1 NAME
176
177 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
178
179 =head1 VERSION
180
181 This document describes Mouse version 0.50_04
182
183 =head1 SEE ALSO
184
185 L<Moose::Meta::Method::Accessor>
186
187 =cut