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