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