Checking in changes prior to tagging of version 0.40_06. Changelog diff is:
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util; # 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{
10     my ($method_class, $attribute, $class, $type) = @_;
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     $type ||= 'accessor';
27
28     my $accessor = sprintf(qq{#line 1 "%s for %s (%s)"\n}, $type, $name, __FILE__)
29                  . "sub {\n";
30
31     if ($type eq 'accessor' || $type eq 'writer') {
32         if($type eq 'accessor'){
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->verify_type_constraint_error($name, '.$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 'reader') {
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->verify_type_constraint_error(\$name, \$tmp, \$constraint);\n";
102             $accessor .= "$slot = \$tmp;\n";
103         }
104         else{
105             $accessor .= "$slot = $value;\n";
106         }
107         $accessor .= "}\n";
108     }
109
110     if ($should_deref) {
111         if ($constraint->is_a_type_of('ArrayRef')) {
112             $accessor .= "return \@{ $slot || [] } if wantarray;\n";
113         }
114         elsif($constraint->is_a_type_of('HashRef')){
115             $accessor .= "return \%{ $slot || {} } if wantarray;\n";
116         }
117         else{
118             $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
119         }
120     }
121
122     $accessor .= "return $slot;\n}\n";
123
124     #print "# class ", $class->name, "\n", $accessor, "\n";
125     my $code;
126     my $e = do{
127         local $@;
128         $code = eval $accessor;
129         $@;
130     };
131     die $e if $e;
132
133     return $code;
134 }
135
136 sub _generate_reader{
137     my $class = shift;
138     return $class->_generate_accessor(@_, 'reader');
139 }
140
141 sub _generate_writer{
142     my $class = shift;
143     return $class->_generate_accessor(@_, 'writer');
144 }
145
146
147 sub _generate_predicate {
148     my (undef, $attribute, $class) = @_;
149
150     my $slot = $attribute->name;
151     return sub{
152         return exists $_[0]->{$slot};
153     };
154 }
155
156 sub _generate_clearer {
157     my (undef, $attribute, $class) = @_;
158
159     my $slot = $attribute->name;
160
161    return sub{
162         delete $_[0]->{$slot};
163     };
164 }
165
166 1;
167 __END__
168
169 =head1 NAME
170
171 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
172
173 =head1 VERSION
174
175 This document describes Mouse version 0.40_06
176
177 =head1 SEE ALSO
178
179 L<Moose::Meta::Method::Accessor>
180
181 =cut