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