Update changes for default default fixes
[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
5use List::MoreUtils qw( any );
e3c07b19 6use Moose::Util::TypeConstraints;
34d6d196 7use Moose::Deprecated;
e3c07b19 8
efa728b4 9our $VERSION = '1.15';
e3c07b19 10$VERSION = eval $VERSION;
11our $AUTHORITY = 'cpan:STEVAN';
12
2e069f5a 13requires '_helper_type';
e3c07b19 14
2edb73d9 15before '_process_options' => sub {
16 my ( $self, $name, $options ) = @_;
e3c07b19 17
2edb73d9 18 $self->_check_helper_type( $options, $name );
e3c07b19 19
3cf2f9ec 20 if ( !( any { exists $options->{$_} } qw( is reader writer accessor ) )
21 && $self->can('_default_is') ) {
22
34d6d196 23 $options->{is} = $self->_default_is;
24
25 Moose::Deprecated::deprecated(
26 feature => 'default is for Native Trait',
27 message =>
28 q{Allowing a native trait to automatically supply a value for "is" is deprecated}
29 );
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 =>
45 'Allowing a native trait to automatically supply a default is deprecated'
46 );
47 }
2edb73d9 48};
e3c07b19 49
2edb73d9 50sub _check_helper_type {
51 my ( $self, $options, $name ) = @_;
e3c07b19 52
2e069f5a 53 my $type = $self->_helper_type;
2edb73d9 54
2e069f5a 55 $options->{isa} = $type
2edb73d9 56 unless exists $options->{isa};
57
58 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
59 $options->{isa} );
60
61 ( $isa->is_a_type_of($type) )
62 || confess
63 "The type constraint for $name must be a subtype of $type but it's a $isa";
64}
e3c07b19 65
2edb73d9 66before 'install_accessors' => sub { (shift)->_check_handles_values };
5404f169 67
2edb73d9 68sub _check_handles_values {
e3c07b19 69 my $self = shift;
70
5404f169 71 my %handles = $self->_canonicalize_handles;
e3c07b19 72
046c8b5e 73 for my $original_method ( values %handles ) {
5404f169 74 my $name = $original_method->[0];
f7fd22b6 75
ffc2e25f 76 my $accessor_class = $self->_native_accessor_class_for($name);
f7fd22b6 77
78aee58f 78 ( $accessor_class && $accessor_class->can('new') )
79 || confess
80 "$name is an unsupported method type - $accessor_class";
e3c07b19 81 }
5404f169 82}
e3c07b19 83
d4dc38ed 84around '_canonicalize_handles' => sub {
85 shift;
86 my $self = shift;
87 my $handles = $self->handles;
88
89 return unless $handles;
90
91 unless ( 'HASH' eq ref $handles ) {
92 $self->throw_error(
93 "The 'handles' option must be a HASH reference, not $handles");
94 }
95
96 return map {
97 my $to = $handles->{$_};
98 $to = [$to] unless ref $to;
99 $_ => $to
100 } keys %$handles;
101};
102
18281451 103around '_make_delegation_method' => sub {
104 my $next = shift;
046c8b5e 105 my ( $self, $handle_name, $method_to_call ) = @_;
18281451 106
3c573ca4 107 my ( $name, @curried_args ) = @$method_to_call;
18281451 108
ffc2e25f 109 my $accessor_class = $self->_native_accessor_class_for($name);
18281451 110
78aee58f 111 die "Cannot find an accessor class for $name"
112 unless $accessor_class && $accessor_class->can('new');
113
114 return $accessor_class->new(
8b9641b8 115 name => $handle_name,
116 package_name => $self->associated_class->name,
117 delegate_to_method => $name,
118 attribute => $self,
119 is_inline => 1,
120 curried_arguments => \@curried_args,
121 root_types => [ $self->_root_types ],
78aee58f 122 );
18281451 123};
124
a6ae7438 125sub _root_types {
126 return $_[0]->_helper_type;
127}
128
ffc2e25f 129sub _native_accessor_class_for {
130 my ( $self, $suffix ) = @_;
131
8b9641b8 132 my $role
133 = 'Moose::Meta::Method::Accessor::Native::'
134 . $self->_native_type . '::'
135 . $suffix;
136
137 return Moose::Meta::Class->create_anon_class(
138 superclasses =>
139 [ $self->accessor_metaclass, $self->delegation_metaclass ],
140 roles => [$role],
141 cache => 1,
142 )->name;
f7fd22b6 143}
144
15715245 145sub _build_native_type {
146 my $self = shift;
147
148 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
149 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
150 }
151
152 die "Cannot calculate native type for " . ref $self;
153}
154
155has '_native_type' => (
156 is => 'ro',
157 isa => 'Str',
158 lazy => 1,
159 builder => '_build_native_type',
160);
161
e3c07b19 162no Moose::Role;
163no Moose::Util::TypeConstraints;
164
1651;
166
167__END__
168
169=head1 NAME
170
e132fd56 171Moose::Meta::Attribute::Native::Trait - Shared role for native delegation traits
e3c07b19 172
e3c07b19 173=head1 BUGS
174
d4048ef3 175See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 176
1af5d116 177=head1 SEE ALSO
178
e132fd56 179Documentation for Moose native traits can be found in
180L<Moose::Meta::Attribute::Native>.
1af5d116 181
e3c07b19 182=head1 AUTHORS
183
184Yuval Kogman
185
186Shawn M Moore
187
188Jesse Luehrs
189
190=head1 COPYRIGHT AND LICENSE
191
192Copyright 2007-2009 by Infinity Interactive, Inc.
193
194L<http://www.iinteractive.com>
195
196This library is free software; you can redistribute it and/or modify
197it under the same terms as Perl itself.
198
199=cut