2 package Moose::Meta::Method::Accessor;
9 use base 'Moose::Meta::Method';
10 'Class::MOP::Method::Accessor';
14 sub generate_predicate_method {
16 my $attr = $self->associated_attribute;
17 my $attr_name = $attr->name;
20 sub generate_clearer_method {
22 my $attr = $self->associated_attribute;
23 my $attr_name = $attr->name;
26 sub generate_accessor_method {
28 my $attr = $self->associated_attribute;
29 my $attr_name = $attr->name;
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;
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)
43 . $self->_inline_check_lazy
44 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv))
48 # set up the environment
49 my $type_constraint = $attr->type_constraint
50 ? $attr->type_constraint->_compiled_type_constraint
54 confess "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@;
58 sub generate_writer_method {
60 my $attr = $self->associated_attribute;
61 my $attr_name = $attr->name;
63 my $value_name = $attr->should_coerce ? '$val' : '$_[1]';
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)
74 # set up the environment
75 my $type_constraint = $attr->type_constraint
76 ? $attr->type_constraint->_compiled_type_constraint
80 confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
84 sub generate_reader_method {
86 my $attr = $self->associated_attribute;
87 my $attr_name = $attr->name;
89 my $attr_name = $attr->slots;
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}' ) . ';'
96 confess "Could not create reader for '$attr_name' because $@ \n code: $code" if $@;
102 sub generate_accessor_method_inline {
104 my $attr = $self->associated_attribute;
105 my $attr_name = $attr->name;
106 my $meta_instance = $attr->associated_class->instance_metaclass;
109 sub generate_reader_method_inline {
111 my $attr = $self->associated_attribute;
112 my $attr_name = $attr->name;
113 my $meta_instance = $attr->associated_class->instance_metaclass;
116 sub generate_writer_method_inline {
118 my $attr = $self->associated_attribute;
119 my $attr_name = $attr->name;
120 my $meta_instance = $attr->associated_class->instance_metaclass;
124 sub generate_predicate_method_inline {
126 my $attr = $self->associated_attribute;
127 my $attr_name = $attr->name;
128 my $meta_instance = $attr->associated_class->instance_metaclass;
131 sub generate_clearer_method_inline {
133 my $attr = $self->associated_attribute;
134 my $attr_name = $attr->name;
135 my $meta_instance = $attr->associated_class->instance_metaclass;
141 sub _inline_check_constraint {
142 my ($self, $value) = @_;
143 return '' unless $self->has_type_constraint;
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")
154 sub _inline_check_coercion {
156 return '' unless $self->should_coerce;
157 return 'my $val = $attr->type_constraint->coerce($_[1]);'
160 sub _inline_check_required {
162 return '' unless $self->is_required;
163 return 'defined($_[1]) || confess "Attribute ($attr_name) is required, so cannot be set to undef";'
166 sub _inline_check_lazy {
168 return '' unless $self->is_lazy;
169 if ($self->has_type_constraint) {
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; ' .
183 ' $_[0]->{$attr_name} = undef;' .
187 return '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)'
188 . 'unless exists $_[0]->{$attr_name};';
193 my ($self, $instance, $value) = @_;
195 my $mi = $self->associated_class->get_meta_instance;
196 my $slot_name = sprintf "'%s'", $self->slots;
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;
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);
211 my ($self, $instance) = @_;
213 my $mi = $self->associated_class->get_meta_instance;
214 my $slot_name = sprintf "'%s'", $self->slots;
216 return $mi->inline_get_slot_value($instance, $slot_name);
219 sub _inline_auto_deref {
220 my ( $self, $ref_value ) = @_;
222 return $ref_value unless $self->should_auto_deref;
224 my $type_constraint = $self->type_constraint;
227 if ($type_constraint->is_a_type_of('ArrayRef')) {
230 elsif ($type_constraint->is_a_type_of('HashRef')) {
234 confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
237 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";