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