99c1e3d3940b17206ad932c5da3de7f02e3f09aa
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util; # enables strict and warnings
3 use Scalar::Util qw(blessed);
4
5 sub _inline_slot{
6     my(undef, $self_var, $attr_name) = @_;
7     return sprintf '%s->{q{%s}}', $self_var, $attr_name;
8 }
9
10 sub _generate_accessor{
11     my ($method_class, $attribute, $class, $type) = @_;
12
13     my $name          = $attribute->name;
14     my $default       = $attribute->default;
15     my $constraint    = $attribute->type_constraint;
16     my $builder       = $attribute->builder;
17     my $trigger       = $attribute->trigger;
18     my $is_weak       = $attribute->is_weak_ref;
19     my $should_deref  = $attribute->should_auto_deref;
20     my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
21
22     my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
23
24     my $self  = '$_[0]';
25     my $slot  = $method_class->_inline_slot($self, $name);;
26
27     $type ||= 'accessor';
28
29     my $accessor = sprintf(qq{#line 1 "%s for %s (%s)"\n}, $type, $name, __FILE__)
30                  . "sub {\n";
31
32     if ($type eq 'accessor' || $type eq 'writer') {
33         if($type eq 'accessor'){
34             $accessor .= 
35                 'if (scalar(@_) >= 2) {' . "\n";
36         }
37         else{ # writer
38             $accessor .= 
39                 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
40                 '{' . "\n";
41         }
42                 
43         my $value = '$_[1]';
44
45         if (defined $constraint) {
46             if ($should_coerce) {
47                 $accessor .=
48                     "\n".
49                     'my $val = $constraint->coerce('.$value.');';
50                 $value = '$val';
51             }
52             $accessor .= 
53                 "\n".
54                 '$compiled_type_constraint->('.$value.') or
55                     $attribute->verify_type_constraint_error($name, '.$value.', $constraint);' . "\n";
56         }
57
58         # if there's nothing left to do for the attribute we can return during
59         # this setter
60         $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
61
62         $accessor .= "$slot = $value;\n";
63
64         if ($is_weak) {
65             $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
66         }
67
68         if ($trigger) {
69             $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
70         }
71
72         $accessor .= "}\n";
73     }
74     elsif($type eq 'reader') {
75         $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
76     }
77     else{
78         $class->throw_error("Unknown accessor type '$type'");
79     }
80
81     if ($attribute->is_lazy) {
82         my $value;
83
84         if (defined $builder){
85             $value = "$self->\$builder()";
86         }
87         elsif (ref($default) eq 'CODE'){
88             $value = "$self->\$default()";
89         }
90         else{
91             $value = '$default';
92         }
93
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->verify_type_constraint_error(\$name, \$tmp, \$constraint);\n";
103             $accessor .= "$slot = \$tmp;\n";
104         }
105         else{
106             $accessor .= "$slot = $value;\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 "# class ", $class->name, "\n", $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_reader{
138     my $class = shift;
139     return $class->_generate_accessor(@_, 'reader');
140 }
141
142 sub _generate_writer{
143     my $class = shift;
144     return $class->_generate_accessor(@_, 'writer');
145 }
146
147
148 sub _generate_predicate {
149     my (undef, $attribute, $class) = @_;
150
151     my $slot = $attribute->name;
152     return sub{
153         return exists $_[0]->{$slot};
154     };
155 }
156
157 sub _generate_clearer {
158     my (undef, $attribute, $class) = @_;
159
160     my $slot = $attribute->name;
161
162    return sub{
163         delete $_[0]->{$slot};
164     };
165 }
166
167 1;
168 __END__
169
170 =head1 NAME
171
172 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
173
174 =head1 VERSION
175
176 This document describes Mouse version 0.40_03
177
178 =head1 SEE ALSO
179
180 L<Moose::Meta::Method::Accessor>
181
182 =cut