restructure this method a bit for readability
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor.pm
1
2 package Moose::Meta::Method::Accessor;
3
4 use strict;
5 use warnings;
6
7 use Try::Tiny;
8
9 use base 'Moose::Meta::Method',
10          'Class::MOP::Method::Accessor';
11
12 sub _error_thrower {
13     my $self = shift;
14     return $self->associated_attribute
15         if ref($self) && defined($self->associated_attribute);
16     return $self->SUPER::_error_thrower;
17 }
18
19 sub _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     };
33 }
34
35 sub _eval_environment {
36     my $self = shift;
37
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
50         # is this going to be an issue? it's currently used for coercions
51         # and the tc message, is there a way to inline those too?
52         $env->{'$type_constraint_obj'} = \$tc_obj;
53         $env->{'$type_constraint'}     = \($tc_obj->_compiled_type_constraint)
54             unless $tc_obj->can_be_inlined;
55
56         $env = { %$env, %{ $tc_obj->inline_environment } };
57     }
58
59     # XXX ugh, fix these
60     $env->{'$attr'} = \$attr
61         if $attr->has_initializer;
62     $env->{'$meta'} = \$self;
63
64     return $env;
65 }
66
67 sub _instance_is_inlinable {
68     my $self = shift;
69     return $self->associated_attribute->associated_class->instance_metaclass->is_inlinable;
70 }
71
72 sub _generate_reader_method {
73     my $self = shift;
74     $self->_instance_is_inlinable ? $self->_generate_reader_method_inline(@_)
75                                   : $self->SUPER::_generate_reader_method(@_);
76 }
77
78 sub _generate_writer_method {
79     my $self = shift;
80     $self->_instance_is_inlinable ? $self->_generate_writer_method_inline(@_)
81                                   : $self->SUPER::_generate_writer_method(@_);
82 }
83
84 sub _generate_accessor_method {
85     my $self = shift;
86     $self->_instance_is_inlinable ? $self->_generate_accessor_method_inline(@_)
87                                   : $self->SUPER::_generate_accessor_method(@_);
88 }
89
90 sub _generate_predicate_method {
91     my $self = shift;
92     $self->_instance_is_inlinable ? $self->_generate_predicate_method_inline(@_)
93                                   : $self->SUPER::_generate_predicate_method(@_);
94 }
95
96 sub _generate_clearer_method {
97     my $self = shift;
98     $self->_instance_is_inlinable ? $self->_generate_clearer_method_inline(@_)
99                                   : $self->SUPER::_generate_clearer_method(@_);
100 }
101
102 sub _writer_value_needs_copy {
103     shift->associated_attribute->_writer_value_needs_copy(@_);
104 }
105
106 sub _inline_tc_code {
107     shift->associated_attribute->_inline_tc_code(@_);
108 }
109
110 sub _inline_check_constraint {
111     shift->associated_attribute->_inline_check_constraint(@_);
112 }
113
114 sub _inline_check_lazy {
115     shift->associated_attribute->_inline_check_lazy(@_);
116 }
117
118 sub _inline_store_value {
119     shift->associated_attribute->_inline_instance_set(@_) . ';';
120 }
121
122 sub _inline_get_old_value_for_trigger {
123     shift->associated_attribute->_inline_get_old_value_for_trigger(@_);
124 }
125
126 sub _inline_trigger {
127     shift->associated_attribute->_inline_trigger(@_);
128 }
129
130 sub _get_value {
131     shift->associated_attribute->_inline_instance_get(@_);
132 }
133
134 sub _has_value {
135     shift->associated_attribute->_inline_instance_has(@_);
136 }
137
138 1;
139
140 # ABSTRACT: A Moose Method metaclass for accessors
141
142 __END__
143
144 =pod
145
146 =head1 DESCRIPTION
147
148 This class is a subclass of L<Class::MOP::Method::Accessor> that
149 provides additional Moose-specific functionality, all of which is
150 private.
151
152 To understand this class, you should read the the
153 L<Class::MOP::Method::Accessor> documentation.
154
155 =head1 BUGS
156
157 See L<Moose/BUGS> for details on reporting bugs.
158
159 =cut