Commit | Line | Data |
8ee73eeb |
1 | |
39b3bc94 |
2 | use lib '/Users/stevan/Projects/Moose/Moose/Class-MOP/trunk/lib'; |
3 | |
8ee73eeb |
4 | package Moose::Meta::Method::Accessor; |
5 | |
6 | use strict; |
7 | use warnings; |
8 | |
39b3bc94 |
9 | use Carp 'confess'; |
10 | |
8ee73eeb |
11 | our $VERSION = '0.01'; |
12 | |
39b3bc94 |
13 | use base 'Moose::Meta::Method', |
d617b644 |
14 | 'Class::MOP::Method::Accessor'; |
15 | |
946289d1 |
16 | ## Inline method generators |
d617b644 |
17 | |
946289d1 |
18 | sub generate_accessor_method_inline { |
d617b644 |
19 | my $self = shift; |
20 | my $attr = $self->associated_attribute; |
21 | my $attr_name = $attr->name; |
22 | |
23 | my $value_name = $attr->should_coerce ? '$val' : '$_[1]'; |
24 | my $mi = $attr->associated_class->get_meta_instance; |
25 | my $slot_name = sprintf "'%s'", $attr->slots; |
26 | my $inv = '$_[0]'; |
27 | my $code = 'sub { ' |
28 | . 'if (scalar(@_) == 2) {' |
29 | . $self->_inline_check_required |
30 | . $self->_inline_check_coercion |
31 | . $self->_inline_check_constraint($value_name) |
32 | . $self->_inline_store($inv, $value_name) |
33 | . $self->_inline_trigger($inv, $value_name) |
34 | . ' }' |
35 | . $self->_inline_check_lazy |
36 | . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) |
37 | . ' }'; |
38 | |
39 | # NOTE: |
40 | # set up the environment |
41 | my $type_constraint = $attr->type_constraint |
42 | ? $attr->type_constraint->_compiled_type_constraint |
43 | : undef; |
44 | |
45 | my $sub = eval $code; |
46 | confess "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@; |
47 | return $sub; |
48 | } |
49 | |
946289d1 |
50 | sub generate_writer_method_inline { |
d617b644 |
51 | my $self = shift; |
52 | my $attr = $self->associated_attribute; |
53 | my $attr_name = $attr->name; |
54 | |
55 | my $value_name = $attr->should_coerce ? '$val' : '$_[1]'; |
56 | my $inv = '$_[0]'; |
57 | my $code = 'sub { ' |
58 | . $self->_inline_check_required |
59 | . $self->_inline_check_coercion |
60 | . $self->_inline_check_constraint($value_name) |
61 | . $self->_inline_store($inv, $value_name) |
62 | . $self->_inline_trigger($inv, $value_name) |
63 | . ' }'; |
64 | |
65 | # NOTE: |
66 | # set up the environment |
67 | my $type_constraint = $attr->type_constraint |
68 | ? $attr->type_constraint->_compiled_type_constraint |
69 | : undef; |
70 | |
71 | my $sub = eval $code; |
72 | confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@; |
73 | return $sub; |
74 | } |
75 | |
946289d1 |
76 | sub generate_reader_method_inline { |
d617b644 |
77 | my $self = shift; |
78 | my $attr = $self->associated_attribute; |
79 | my $attr_name = $attr->name; |
80 | |
d617b644 |
81 | my $code = 'sub {' |
82 | . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;' |
83 | . $self->_inline_check_lazy |
84 | . 'return ' . $self->_inline_auto_deref( '$_[0]->{$attr_name}' ) . ';' |
85 | . '}'; |
86 | my $sub = eval $code; |
87 | confess "Could not create reader for '$attr_name' because $@ \n code: $code" if $@; |
88 | return $sub; |
89 | } |
90 | |
39b3bc94 |
91 | ## ... private helpers |
d617b644 |
92 | |
93 | sub _inline_check_constraint { |
94 | my ($self, $value) = @_; |
39b3bc94 |
95 | |
96 | my $attr = $self->associated_attribute; |
97 | |
98 | return '' unless $attr->has_type_constraint; |
d617b644 |
99 | |
100 | # FIXME - remove 'unless defined($value) - constraint Undef |
101 | return sprintf <<'EOF', $value, $value, $value, $value |
102 | defined($type_constraint->(%s)) |
103 | || confess "Attribute (" . $attr->name . ") does not pass the type constraint (" |
104 | . $attr->type_constraint->name . ") with " . (defined(%s) ? "'%s'" : "undef") |
105 | if defined(%s); |
106 | EOF |
107 | } |
108 | |
109 | sub _inline_check_coercion { |
39b3bc94 |
110 | my $attr = (shift)->associated_attribute; |
111 | |
112 | return '' unless $attr->should_coerce; |
d617b644 |
113 | return 'my $val = $attr->type_constraint->coerce($_[1]);' |
114 | } |
115 | |
116 | sub _inline_check_required { |
39b3bc94 |
117 | my $attr = (shift)->associated_attribute; |
118 | |
119 | return '' unless $attr->is_required; |
d617b644 |
120 | return 'defined($_[1]) || confess "Attribute ($attr_name) is required, so cannot be set to undef";' |
121 | } |
122 | |
123 | sub _inline_check_lazy { |
39b3bc94 |
124 | my $attr = (shift)->associated_attribute; |
125 | |
126 | return '' unless $attr->is_lazy; |
127 | |
128 | if ($attr->has_type_constraint) { |
d617b644 |
129 | # NOTE: |
130 | # this could probably be cleaned |
131 | # up and streamlined a little more |
132 | return 'unless (exists $_[0]->{$attr_name}) {' . |
133 | ' if ($attr->has_default) {' . |
134 | ' my $default = $attr->default($_[0]);' . |
135 | ' (defined($type_constraint->($default)))' . |
136 | ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' . |
137 | ' . $attr->type_constraint->name . ") with " . (defined($default) ? "\'$default\'" : "undef")' . |
138 | ' if defined($default);' . |
139 | ' $_[0]->{$attr_name} = $default; ' . |
140 | ' }' . |
141 | ' else {' . |
142 | ' $_[0]->{$attr_name} = undef;' . |
143 | ' }' . |
144 | '}'; |
145 | } |
146 | return '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)' |
147 | . 'unless exists $_[0]->{$attr_name};'; |
148 | } |
149 | |
150 | |
151 | sub _inline_store { |
152 | my ($self, $instance, $value) = @_; |
39b3bc94 |
153 | my $attr = $self->associated_attribute; |
d617b644 |
154 | |
39b3bc94 |
155 | my $mi = $attr->associated_class->get_meta_instance; |
156 | my $slot_name = sprintf "'%s'", $attr->slots; |
d617b644 |
157 | |
158 | my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";"; |
159 | $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";" |
39b3bc94 |
160 | if $attr->is_weak_ref; |
d617b644 |
161 | return $code; |
162 | } |
163 | |
164 | sub _inline_trigger { |
165 | my ($self, $instance, $value) = @_; |
39b3bc94 |
166 | my $attr = $self->associated_attribute; |
167 | return '' unless $attr->has_trigger; |
d617b644 |
168 | return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value); |
169 | } |
170 | |
171 | sub _inline_get { |
172 | my ($self, $instance) = @_; |
39b3bc94 |
173 | my $attr = $self->associated_attribute; |
d617b644 |
174 | |
39b3bc94 |
175 | my $mi = $attr->associated_class->get_meta_instance; |
176 | my $slot_name = sprintf "'%s'", $attr->slots; |
d617b644 |
177 | |
178 | return $mi->inline_get_slot_value($instance, $slot_name); |
179 | } |
180 | |
181 | sub _inline_auto_deref { |
182 | my ( $self, $ref_value ) = @_; |
39b3bc94 |
183 | my $attr = $self->associated_attribute; |
d617b644 |
184 | |
39b3bc94 |
185 | return $ref_value unless $attr->should_auto_deref; |
d617b644 |
186 | |
39b3bc94 |
187 | my $type_constraint = $attr->type_constraint; |
d617b644 |
188 | |
189 | my $sigil; |
190 | if ($type_constraint->is_a_type_of('ArrayRef')) { |
191 | $sigil = '@'; |
192 | } |
193 | elsif ($type_constraint->is_a_type_of('HashRef')) { |
194 | $sigil = '%'; |
195 | } |
196 | else { |
197 | confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'"; |
198 | } |
199 | |
200 | "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )"; |
201 | } |
8ee73eeb |
202 | |
203 | 1; |
204 | |
205 | __END__ |
206 | |
207 | =pod |
208 | |
39b3bc94 |
209 | =head1 NAME |
210 | |
211 | =head1 SYNOPOSIS |
212 | |
213 | =head1 DESCRIPTION |
214 | |
215 | =head1 METHODS |
216 | |
217 | =over 4 |
218 | |
219 | =item B<generate_accessor_method> |
220 | |
221 | =item B<generate_accessor_method_inline> |
222 | |
223 | =item B<generate_reader_method> |
224 | |
225 | =item B<generate_reader_method_inline> |
226 | |
227 | =item B<generate_writer_method> |
228 | |
229 | =item B<generate_writer_method_inline> |
230 | |
231 | =back |
232 | |
233 | =head1 BUGS |
234 | |
235 | All complex software has bugs lurking in it, and this module is no |
236 | exception. If you find a bug please either email me, or add the bug |
237 | to cpan-RT. |
238 | |
239 | =head1 AUTHOR |
240 | |
241 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
242 | |
243 | Yuval Kogman E<lt>nothingmuch@woobling.comE<gt> |
244 | |
245 | =head1 COPYRIGHT AND LICENSE |
246 | |
247 | Copyright 2006 by Infinity Interactive, Inc. |
248 | |
249 | L<http://www.iinteractive.com> |
250 | |
251 | This library is free software; you can redistribute it and/or modify |
252 | it under the same terms as Perl itself. |
253 | |
8ee73eeb |
254 | =cut |