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