make sure all modules have the same version
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Writer.pm
1 package Moose::Meta::Method::Accessor::Native::Writer;
2
3 use strict;
4 use warnings;
5
6 use List::MoreUtils qw( any );
7
8 our $VERSION = '1.14';
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
11
12 use base 'Moose::Meta::Method::Accessor::Native';
13
14 sub _generate_method {
15     my $self = shift;
16
17     my $inv = '$self';
18
19     my $slot_access = $self->_inline_get($inv);
20
21     my $code = 'sub {';
22
23     $code .= "\n" . $self->_inline_pre_body(@_);
24
25     $code .= "\n" . 'my $self = shift;';
26
27     $code .= "\n" . $self->_inline_curried_arguments;
28
29     $code .= $self->_writer_core( $inv, $slot_access );
30
31     $code .= "\n" . $self->_inline_post_body(@_);
32
33     $code .= "\n}";
34
35     return $code;
36 }
37
38 sub _writer_core {
39     my ( $self, $inv, $slot_access ) = @_;
40
41     my $code = q{};
42
43     $code .= "\n" . $self->_inline_check_argument_count;
44     $code .= "\n" . $self->_inline_process_arguments( $inv, $slot_access );
45     $code .= "\n" . $self->_inline_check_arguments('for writer');
46
47     $code .= "\n" . $self->_inline_check_lazy($inv);
48
49     my $potential_value = $self->_potential_value($slot_access);
50
51     $code .= "\n" . $self->_inline_copy_value( \$potential_value );
52     $code .= "\n"
53         . $self->_inline_tc_code(
54         $potential_value
55         );
56
57     $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
58     $code .= "\n" . $self->_inline_capture_return_value($slot_access);
59     $code .= "\n"
60         . $self->_inline_set_new_value(
61         $inv,
62         $potential_value,
63         $slot_access,
64         );
65     $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' );
66     $code .= "\n" . $self->_return_value( $slot_access, 'for writer' );
67
68     return $code;
69 }
70
71 sub _inline_process_arguments {q{}}
72
73 sub _inline_check_arguments {q{}}
74
75 sub _value_needs_copy {
76     my $self = shift;
77
78     return $self->_constraint_must_be_checked;
79 }
80
81 sub _constraint_must_be_checked {
82     my $self = shift;
83
84     my $attr = $self->associated_attribute;
85
86     return $attr->has_type_constraint
87         && ( !$self->_is_root_type( $attr->type_constraint )
88         || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
89 }
90
91 sub _is_root_type {
92     my ($self, $type) = @_;
93
94     my $name = $type->name();
95
96     return any { $name eq $_ } @{ $self->root_types };
97 }
98
99 sub _inline_copy_value {
100     my ( $self, $potential_ref ) = @_;
101
102     return q{} unless $self->_value_needs_copy;
103
104     my $code = "my \$potential = ${$potential_ref};";
105
106     ${$potential_ref} = '$potential';
107
108     return $code;
109 }
110
111 sub _inline_tc_code {
112     my ( $self, $potential_value ) = @_;
113
114     return q{} unless $self->_constraint_must_be_checked;
115
116     return $self->_inline_check_coercion($potential_value) . "\n"
117         . $self->_inline_check_constraint($potential_value);
118 }
119
120 sub _inline_check_coercion {
121     my ( $self, $value ) = @_;
122
123     my $attr = $self->associated_attribute;
124
125     return ''
126         unless $attr->should_coerce && $attr->type_constraint->has_coercion;
127
128     # We want to break the aliasing in @_ in case the coercion tries to make a
129     # destructive change to an array member.
130     return "$value = \$type_constraint_obj->coerce($value);";
131 }
132
133 sub _inline_check_constraint {
134     my $self = shift;
135
136     return q{} unless $self->_constraint_must_be_checked;
137
138     return $self->SUPER::_inline_check_constraint( $_[0] );
139 }
140
141 sub _inline_capture_return_value { return q{} }
142
143 sub _inline_set_new_value {
144     my $self = shift;
145
146     return $self->SUPER::_inline_store(@_)
147         if $self->_value_needs_copy;
148
149     return $self->_inline_optimized_set_new_value(@_);
150 }
151
152 sub _inline_optimized_set_new_value {
153     my $self = shift;
154
155     return $self->SUPER::_inline_store(@_)
156 }
157
158 sub _return_value { return q{} }
159
160 1;