7be4ec9929c3cfb5b5ac28a23c759fd2b12befea
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
1 package Mouse::Meta::Method::Accessor;
2 use strict;
3 use warnings;
4 use Scalar::Util qw(blessed);
5
6 sub _generate_accessor{
7     my (undef, $attribute, $class, $type) = @_;
8
9     my $name          = $attribute->name;
10     my $default       = $attribute->default;
11     my $constraint    = $attribute->type_constraint;
12     my $builder       = $attribute->builder;
13     my $trigger       = $attribute->trigger;
14     my $is_weak       = $attribute->is_weak_ref;
15     my $should_deref  = $attribute->should_auto_deref;
16     my $should_coerce = $attribute->should_coerce;
17
18     my $compiled_type_constraint = $constraint ? $constraint->_compiled_type_constraint : undef;
19
20     my $self  = '$_[0]';
21     my $key   = sprintf q{"%s"}, quotemeta $name;
22
23     $type ||= 'accessor';
24
25     my $accessor = 
26         '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
27         "sub {\n";
28
29     if ($type eq 'accessor' || $type eq 'writer') {
30         if($type eq 'accessor'){
31             $accessor .= 
32                 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
33                 'if (scalar(@_) >= 2) {' . "\n";
34         }
35         else{ # writer
36             $accessor .= 
37                 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
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(!$compiled_type_constraint){
46                 Carp::confess("[BUG] Missing compiled type constraint for $constraint");
47             }
48             if ($should_coerce) {
49                 $accessor .=
50                     "\n".
51                     '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
52                     'my $val = $constraint->coerce('.$value.');';
53                 $value = '$val';
54             }
55             $accessor .= 
56                 "\n".
57                 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
58                 'unless ($compiled_type_constraint->('.$value.')) {
59                     $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
60                 }' . "\n";
61         }
62
63         # if there's nothing left to do for the attribute we can return during
64         # this setter
65         $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
66
67         $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
68
69         if ($is_weak) {
70             $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
71         }
72
73         if ($trigger) {
74             $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
75         }
76
77         $accessor .= "}\n";
78     }
79     elsif($type eq 'reader') {
80         $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
81     }
82     else{
83         $class->throw_error("Unknown accessor type '$type'");
84     }
85
86     if ($attribute->is_lazy) {
87         $accessor .= $self.'->{'.$key.'} = ';
88
89         if($should_coerce && defined($constraint)){
90             $accessor .= '$attribute->_coerce_and_verify(';
91         }
92         $accessor .=   $attribute->has_builder ? $self.'->$builder'
93                      : ref($default) eq 'CODE' ? '$default->('.$self.')'
94                      :                           '$default';
95
96         if($should_coerce && defined $constraint){
97             $accessor .= ')';
98         }
99         $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
100     }
101
102     if ($should_deref) {
103         if ($constraint->is_a_type_of('ArrayRef')) {
104             $accessor .= 'if (wantarray) {
105                 return @{ '.$self.'->{'.$key.'} || [] };
106             }';
107         }
108         elsif($constraint->is_a_type_of('HashRef')){
109             $accessor .= 'if (wantarray) {
110                 return %{ '.$self.'->{'.$key.'} || {} };
111             }';
112         }
113         else{
114             $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
115         }
116     }
117
118     $accessor .= 'return '.$self.'->{'.$key."};\n}";
119
120     #print $accessor, "\n";
121     my $code;
122     my $e = do{
123         local $@;
124         $code = eval $accessor;
125         $@;
126     };
127     die $e if $e;
128
129     return $code;
130 }
131
132 sub _generate_reader{
133     my $class = shift;
134     return $class->_generate_accessor(@_, 'reader');
135 }
136
137 sub _generate_writer{
138     my $class = shift;
139     return $class->_generate_accessor(@_, 'writer');
140 }
141
142
143 sub _generate_predicate {
144     my (undef, $attribute, $class) = @_;
145
146     my $slot = $attribute->name;
147     return sub{
148         return exists $_[0]->{$slot};
149     };
150 }
151
152 sub _generate_clearer {
153     my (undef, $attribute, $class) = @_;
154
155     my $slot = $attribute->name;
156
157    return sub{
158         delete $_[0]->{$slot};
159     };
160 }
161
162 sub _generate_delegation{
163     my (undef, $attribute, $class, $reader, $handle_name, $method_to_call) = @_;
164
165     return sub {
166         my $instance = shift;
167         my $proxy    = $instance->$reader();
168
169         my $error = !defined($proxy)                ? ' is not defined'
170                   : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
171                                                     : undef;
172         if ($error) {
173             $instance->meta->throw_error(
174                 "Cannot delegate $handle_name to $method_to_call because "
175                     . "the value of "
176                     . $attribute->name
177                     . $error
178              );
179         }
180         $proxy->$method_to_call(@_);
181     };
182 }
183
184
185 1;