lib/Moose/Meta/Attribute/Native/Trait.pm: factor out some of the namespace resolution...
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait.pm
CommitLineData
e3c07b19 1
c466e58f 2package Moose::Meta::Attribute::Native::Trait;
e3c07b19 3use Moose::Role;
3cf2f9ec 4
b5ae7c00 5use Class::Load qw(load_class);
efa806d8 6use List::MoreUtils qw( any uniq );
e3c07b19 7use Moose::Util::TypeConstraints;
34d6d196 8use Moose::Deprecated;
e3c07b19 9
2e069f5a 10requires '_helper_type';
e3c07b19 11
efa806d8 12has _used_default_is => (
13 is => 'rw',
14 isa => 'Bool',
15 default => 0,
16);
17
2edb73d9 18before '_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 50after '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 82sub _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 98before 'install_accessors' => sub { (shift)->_check_handles_values };
5404f169 99
2edb73d9 100sub _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 116around '_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 133sub _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 146around '_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 168sub _root_types {
169 return $_[0]->_helper_type;
170}
171
c20a93c6 172
173#
174# Foo::Bar::Baz::Quux::doo
175#
176# ^^^^^^^^^^^^^ - native accessor type prefix
177# ^^^^^^^^^^^^^^^^^^^ - native accessor method prefix
178# ^^^^^^^^^^^^^^^^^^^^^^^^ - native accessor methodclass for ( $suffix = doo )
179
180sub _native_accessor_type_prefix { 'Moose::Meta::Method::Accessor::Native' }
181
182sub _native_accessor_method_prefix {
183 my ( $self, ) = @_;
184 return $self->_native_accessor_type_prefix . '::' . $self->_native_type ;
185}
186sub _native_accessor_methodclass_for {
187 my ( $self, $suffix ) = @_;
188 return $self->_native_accessor_method_prefix . '::' . $suffix;
189}
190
ffc2e25f 191sub _native_accessor_class_for {
192 my ( $self, $suffix ) = @_;
193
c20a93c6 194 my $role = $self->_native_accessor_methodclass_for( $suffix );
8b9641b8 195
b5ae7c00 196 load_class($role);
8b9641b8 197 return Moose::Meta::Class->create_anon_class(
198 superclasses =>
199 [ $self->accessor_metaclass, $self->delegation_metaclass ],
200 roles => [$role],
201 cache => 1,
202 )->name;
f7fd22b6 203}
204
c20a93c6 205sub _native_type_matcher { qr/::Native::Trait::(\w+)$/ }
206
15715245 207sub _build_native_type {
208 my $self = shift;
c20a93c6 209 my $matcher = $self->_native_type_matcher;
15715245 210
211 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
c20a93c6 212
213 return $1 if $role_name =~ $matcher
15715245 214 }
215
216 die "Cannot calculate native type for " . ref $self;
217}
218
219has '_native_type' => (
220 is => 'ro',
221 isa => 'Str',
222 lazy => 1,
223 builder => '_build_native_type',
224);
225
e3c07b19 226no Moose::Role;
227no Moose::Util::TypeConstraints;
228
2291;
230
ad46f524 231# ABSTRACT: Shared role for native delegation traits
e3c07b19 232
ad46f524 233__END__
e3c07b19 234
e3c07b19 235=head1 BUGS
236
d4048ef3 237See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 238
1af5d116 239=head1 SEE ALSO
240
e132fd56 241Documentation for Moose native traits can be found in
242L<Moose::Meta::Attribute::Native>.
1af5d116 243
e3c07b19 244=cut