this only matters for lazy attributes with initializers
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor.pm
CommitLineData
8ee73eeb 1
2package Moose::Meta::Method::Accessor;
3
4use strict;
5use warnings;
6
55c361dc 7use Try::Tiny;
8
39b3bc94 9use base 'Moose::Meta::Method',
d617b644 10 'Class::MOP::Method::Accessor';
11
18748ad6 12sub _error_thrower {
13 my $self = shift;
cc743de9 14 return $self->associated_attribute
15 if ref($self) && defined($self->associated_attribute);
16 return $self->SUPER::_error_thrower;
18748ad6 17}
d617b644 18
55c361dc 19sub _compile_code {
20 my $self = shift;
21 my @args = @_;
22 try {
23 $self->SUPER::_compile_code(@args);
24 }
25 catch {
26 $self->throw_error(
27 'Could not create writer for '
28 . "'" . $self->associated_attribute->name . "' "
29 . 'because ' . $_,
30 error => $_,
31 );
32 };
c9183ffc 33}
34
f5f08b5f 35sub _eval_environment {
36 my $self = shift;
37
144a8df1 38 my $env = { };
39
40 my $attr = $self->associated_attribute;
41
42 $env->{'$trigger'} = \($attr->trigger)
43 if $attr->has_trigger;
44 $env->{'$default'} = \($attr->default)
45 if $attr->has_default;
46
47 if ($attr->has_type_constraint) {
48 my $tc_obj = $attr->type_constraint;
49
c40e4359 50 # is this going to be an issue? it's currently only used for the tc
51 # message. is there a way to inline that too?
144a8df1 52 $env->{'$type_constraint_obj'} = \$tc_obj;
c40e4359 53
54 $env->{'$type_constraint'} = \(
55 $tc_obj->_compiled_type_constraint
56 ) unless $tc_obj->can_be_inlined;
57 $env->{'$type_coercion'} = \(
58 $tc_obj->coercion->_compiled_type_coercion
59 ) if $tc_obj->has_coercion;
144a8df1 60
61 $env = { %$env, %{ $tc_obj->inline_environment } };
62 }
63
64 # XXX ugh, fix these
65 $env->{'$attr'} = \$attr
d1ef0daf 66 if $attr->has_initializer && $attr->is_lazy;
144a8df1 67 $env->{'$meta'} = \$self;
68
69 return $env;
f5f08b5f 70}
71
f001c60f 72sub _instance_is_inlinable {
73 my $self = shift;
74 return $self->associated_attribute->associated_class->instance_metaclass->is_inlinable;
75}
76
77sub _generate_reader_method {
78 my $self = shift;
79 $self->_instance_is_inlinable ? $self->_generate_reader_method_inline(@_)
80 : $self->SUPER::_generate_reader_method(@_);
81}
82
83sub _generate_writer_method {
84 my $self = shift;
85 $self->_instance_is_inlinable ? $self->_generate_writer_method_inline(@_)
86 : $self->SUPER::_generate_writer_method(@_);
87}
88
89sub _generate_accessor_method {
90 my $self = shift;
91 $self->_instance_is_inlinable ? $self->_generate_accessor_method_inline(@_)
92 : $self->SUPER::_generate_accessor_method(@_);
93}
94
95sub _generate_predicate_method {
96 my $self = shift;
97 $self->_instance_is_inlinable ? $self->_generate_predicate_method_inline(@_)
98 : $self->SUPER::_generate_predicate_method(@_);
99}
100
101sub _generate_clearer_method {
102 my $self = shift;
103 $self->_instance_is_inlinable ? $self->_generate_clearer_method_inline(@_)
104 : $self->SUPER::_generate_clearer_method(@_);
105}
8ecb1fa0 106
6e50f7e9 107sub _writer_value_needs_copy {
108 shift->associated_attribute->_writer_value_needs_copy(@_);
109}
c350159f 110
1e2c801e 111sub _inline_tc_code {
6e50f7e9 112 shift->associated_attribute->_inline_tc_code(@_);
1e2c801e 113}
114
0f4afc62 115sub _inline_check_coercion {
116 shift->associated_attribute->_inline_check_coercion(@_);
117}
118
d617b644 119sub _inline_check_constraint {
6e50f7e9 120 shift->associated_attribute->_inline_check_constraint(@_);
d617b644 121}
122
123sub _inline_check_lazy {
6e50f7e9 124 shift->associated_attribute->_inline_check_lazy(@_);
9df136d0 125}
d617b644 126
1e2c801e 127sub _inline_store_value {
6e50f7e9 128 shift->associated_attribute->_inline_instance_set(@_) . ';';
d617b644 129}
130
3dda07f5 131sub _inline_get_old_value_for_trigger {
6e50f7e9 132 shift->associated_attribute->_inline_get_old_value_for_trigger(@_);
3dda07f5 133}
134
d617b644 135sub _inline_trigger {
6e50f7e9 136 shift->associated_attribute->_inline_trigger(@_);
d617b644 137}
138
1e2c801e 139sub _get_value {
6e50f7e9 140 shift->associated_attribute->_inline_instance_get(@_);
d617b644 141}
142
1e2c801e 143sub _has_value {
6e50f7e9 144 shift->associated_attribute->_inline_instance_has(@_);
d617b644 145}
8ee73eeb 146
1471;
148
ad46f524 149# ABSTRACT: A Moose Method metaclass for accessors
150
8ee73eeb 151__END__
152
153=pod
154
39b3bc94 155=head1 DESCRIPTION
156
ba4dfb8e 157This class is a subclass of L<Class::MOP::Method::Accessor> that
73f769fc 158provides additional Moose-specific functionality, all of which is
159private.
ecb59493 160
73f769fc 161To understand this class, you should read the the
ba4dfb8e 162L<Class::MOP::Method::Accessor> documentation.
39b3bc94 163
164=head1 BUGS
165
d4048ef3 166See L<Moose/BUGS> for details on reporting bugs.
39b3bc94 167
51308c23 168=cut