Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait; |
e3c07b19 |
3 | use Moose::Role; |
3cf2f9ec |
4 | |
b5ae7c00 |
5 | use Class::Load qw(load_class); |
efa806d8 |
6 | use List::MoreUtils qw( any uniq ); |
e3c07b19 |
7 | use Moose::Util::TypeConstraints; |
34d6d196 |
8 | use Moose::Deprecated; |
e3c07b19 |
9 | |
2e069f5a |
10 | requires '_helper_type'; |
e3c07b19 |
11 | |
efa806d8 |
12 | has _used_default_is => ( |
13 | is => 'rw', |
14 | isa => 'Bool', |
15 | default => 0, |
16 | ); |
17 | |
2edb73d9 |
18 | before '_process_options' => sub { |
19 | my ( $self, $name, $options ) = @_; |
e3c07b19 |
20 | |
2edb73d9 |
21 | $self->_check_helper_type( $options, $name ); |
e3c07b19 |
22 | |
3cf2f9ec |
23 | if ( !( any { exists $options->{$_} } qw( is reader writer accessor ) ) |
24 | && $self->can('_default_is') ) { |
25 | |
34d6d196 |
26 | $options->{is} = $self->_default_is; |
27 | |
efa806d8 |
28 | $options->{_used_default_is} = 1; |
34d6d196 |
29 | } |
30 | |
3cf2f9ec |
31 | if ( |
32 | !( |
33 | $options->{required} |
b558f8a6 |
34 | || any { exists $options->{$_} } qw( default builder lazy_build ) |
3cf2f9ec |
35 | ) |
36 | && $self->can('_default_default') |
37 | ) { |
38 | |
34d6d196 |
39 | $options->{default} = $self->_default_default; |
40 | |
41 | Moose::Deprecated::deprecated( |
42 | feature => 'default default for Native Trait', |
43 | message => |
cab2e1d0 |
44 | 'Allowing a native trait to automatically supply a default is deprecated.' |
557ae2ff |
45 | . ' You can avoid this warning by supplying a default, builder, or making the attribute required' |
34d6d196 |
46 | ); |
47 | } |
2edb73d9 |
48 | }; |
e3c07b19 |
49 | |
efa806d8 |
50 | after 'install_accessors' => sub { |
51 | my $self = shift; |
52 | |
53 | return unless $self->_used_default_is; |
54 | |
55 | my @methods |
56 | = $self->_default_is eq 'rw' |
57 | ? qw( reader writer accessor ) |
58 | : 'reader'; |
59 | |
60 | my $name = $self->name; |
61 | my $class = $self->associated_class->name; |
62 | |
63 | for my $meth ( uniq grep {defined} map { $self->$_ } @methods ) { |
64 | |
65 | my $message |
66 | = "The $meth method in the $class class was automatically created" |
67 | . " by the native delegation trait for the $name attribute." |
68 | . q{ This "default is" feature is deprecated.} |
69 | . q{ Explicitly set "is" or define accessor names to avoid this}; |
70 | |
71 | $self->associated_class->add_before_method_modifier( |
72 | $meth => sub { |
73 | Moose::Deprecated::deprecated( |
74 | feature => 'default is for Native Trait', |
75 | message =>$message, |
76 | ); |
77 | } |
78 | ); |
79 | } |
80 | }; |
81 | |
2edb73d9 |
82 | sub _check_helper_type { |
83 | my ( $self, $options, $name ) = @_; |
e3c07b19 |
84 | |
2e069f5a |
85 | my $type = $self->_helper_type; |
2edb73d9 |
86 | |
2e069f5a |
87 | $options->{isa} = $type |
2edb73d9 |
88 | unless exists $options->{isa}; |
89 | |
90 | my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint( |
91 | $options->{isa} ); |
92 | |
93 | ( $isa->is_a_type_of($type) ) |
94 | || confess |
95 | "The type constraint for $name must be a subtype of $type but it's a $isa"; |
96 | } |
e3c07b19 |
97 | |
2edb73d9 |
98 | before 'install_accessors' => sub { (shift)->_check_handles_values }; |
5404f169 |
99 | |
2edb73d9 |
100 | sub _check_handles_values { |
e3c07b19 |
101 | my $self = shift; |
102 | |
5404f169 |
103 | my %handles = $self->_canonicalize_handles; |
e3c07b19 |
104 | |
046c8b5e |
105 | for my $original_method ( values %handles ) { |
5404f169 |
106 | my $name = $original_method->[0]; |
f7fd22b6 |
107 | |
ffc2e25f |
108 | my $accessor_class = $self->_native_accessor_class_for($name); |
f7fd22b6 |
109 | |
78aee58f |
110 | ( $accessor_class && $accessor_class->can('new') ) |
111 | || confess |
112 | "$name is an unsupported method type - $accessor_class"; |
e3c07b19 |
113 | } |
5404f169 |
114 | } |
e3c07b19 |
115 | |
d4dc38ed |
116 | around '_canonicalize_handles' => sub { |
117 | shift; |
118 | my $self = shift; |
119 | my $handles = $self->handles; |
120 | |
121 | return unless $handles; |
122 | |
123 | unless ( 'HASH' eq ref $handles ) { |
124 | $self->throw_error( |
125 | "The 'handles' option must be a HASH reference, not $handles"); |
126 | } |
127 | |
b0b04f2b |
128 | return |
129 | map { $_ => $self->_canonicalize_handles_value( $handles->{$_} ) } |
130 | keys %$handles; |
d4dc38ed |
131 | }; |
132 | |
b0b04f2b |
133 | sub _canonicalize_handles_value { |
134 | my $self = shift; |
135 | my $value = shift; |
136 | |
137 | if ( ref $value && 'ARRAY' ne ref $value ) { |
138 | $self->throw_error( |
139 | "All values passed to handles must be strings or ARRAY references, not $value" |
140 | ); |
141 | } |
142 | |
143 | return ref $value ? $value : [$value]; |
144 | } |
145 | |
18281451 |
146 | around '_make_delegation_method' => sub { |
147 | my $next = shift; |
046c8b5e |
148 | my ( $self, $handle_name, $method_to_call ) = @_; |
18281451 |
149 | |
3c573ca4 |
150 | my ( $name, @curried_args ) = @$method_to_call; |
18281451 |
151 | |
ffc2e25f |
152 | my $accessor_class = $self->_native_accessor_class_for($name); |
18281451 |
153 | |
78aee58f |
154 | die "Cannot find an accessor class for $name" |
155 | unless $accessor_class && $accessor_class->can('new'); |
156 | |
157 | return $accessor_class->new( |
8b9641b8 |
158 | name => $handle_name, |
159 | package_name => $self->associated_class->name, |
160 | delegate_to_method => $name, |
161 | attribute => $self, |
162 | is_inline => 1, |
163 | curried_arguments => \@curried_args, |
164 | root_types => [ $self->_root_types ], |
78aee58f |
165 | ); |
18281451 |
166 | }; |
167 | |
a6ae7438 |
168 | sub _root_types { |
169 | return $_[0]->_helper_type; |
170 | } |
171 | |
ffc2e25f |
172 | sub _native_accessor_class_for { |
173 | my ( $self, $suffix ) = @_; |
174 | |
8b9641b8 |
175 | my $role |
176 | = 'Moose::Meta::Method::Accessor::Native::' |
177 | . $self->_native_type . '::' |
178 | . $suffix; |
179 | |
b5ae7c00 |
180 | load_class($role); |
8b9641b8 |
181 | return Moose::Meta::Class->create_anon_class( |
182 | superclasses => |
183 | [ $self->accessor_metaclass, $self->delegation_metaclass ], |
184 | roles => [$role], |
185 | cache => 1, |
186 | )->name; |
f7fd22b6 |
187 | } |
188 | |
15715245 |
189 | sub _build_native_type { |
190 | my $self = shift; |
191 | |
192 | for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) { |
193 | return $1 if $role_name =~ /::Native::Trait::(\w+)$/; |
194 | } |
195 | |
196 | die "Cannot calculate native type for " . ref $self; |
197 | } |
198 | |
199 | has '_native_type' => ( |
200 | is => 'ro', |
201 | isa => 'Str', |
202 | lazy => 1, |
203 | builder => '_build_native_type', |
204 | ); |
205 | |
e3c07b19 |
206 | no Moose::Role; |
207 | no Moose::Util::TypeConstraints; |
208 | |
209 | 1; |
210 | |
ad46f524 |
211 | # ABSTRACT: Shared role for native delegation traits |
e3c07b19 |
212 | |
ad46f524 |
213 | __END__ |
e3c07b19 |
214 | |
e3c07b19 |
215 | =head1 BUGS |
216 | |
d4048ef3 |
217 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
218 | |
1af5d116 |
219 | =head1 SEE ALSO |
220 | |
e132fd56 |
221 | Documentation for Moose native traits can be found in |
222 | L<Moose::Meta::Attribute::Native>. |
1af5d116 |
223 | |
e3c07b19 |
224 | =cut |