2 package Moose::Meta::Attribute::Native::Trait;
5 use List::MoreUtils qw( any uniq );
6 use Moose::Util::TypeConstraints;
9 our $VERSION = '1.9900';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 requires '_helper_type';
15 has _used_default_is => (
21 before '_process_options' => sub {
22 my ( $self, $name, $options ) = @_;
24 $self->_check_helper_type( $options, $name );
26 if ( !( any { exists $options->{$_} } qw( is reader writer accessor ) )
27 && $self->can('_default_is') ) {
29 $options->{is} = $self->_default_is;
31 $options->{_used_default_is} = 1;
37 || any { exists $options->{$_} } qw( default builder lazy_build )
39 && $self->can('_default_default')
42 $options->{default} = $self->_default_default;
44 Moose::Deprecated::deprecated(
45 feature => 'default default for Native Trait',
47 'Allowing a native trait to automatically supply a default is deprecated.'
48 . ' You can avoid this warning by supplying a default, builder, or making the attribute required'
53 after 'install_accessors' => sub {
56 return unless $self->_used_default_is;
59 = $self->_default_is eq 'rw'
60 ? qw( reader writer accessor )
63 my $name = $self->name;
64 my $class = $self->associated_class->name;
66 for my $meth ( uniq grep {defined} map { $self->$_ } @methods ) {
69 = "The $meth method in the $class class was automatically created"
70 . " by the native delegation trait for the $name attribute."
71 . q{ This "default is" feature is deprecated.}
72 . q{ Explicitly set "is" or define accessor names to avoid this};
74 $self->associated_class->add_before_method_modifier(
76 Moose::Deprecated::deprecated(
77 feature => 'default is for Native Trait',
85 sub _check_helper_type {
86 my ( $self, $options, $name ) = @_;
88 my $type = $self->_helper_type;
90 $options->{isa} = $type
91 unless exists $options->{isa};
93 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
96 ( $isa->is_a_type_of($type) )
98 "The type constraint for $name must be a subtype of $type but it's a $isa";
101 before 'install_accessors' => sub { (shift)->_check_handles_values };
103 sub _check_handles_values {
106 my %handles = $self->_canonicalize_handles;
108 for my $original_method ( values %handles ) {
109 my $name = $original_method->[0];
111 my $accessor_class = $self->_native_accessor_class_for($name);
113 ( $accessor_class && $accessor_class->can('new') )
115 "$name is an unsupported method type - $accessor_class";
119 around '_canonicalize_handles' => sub {
122 my $handles = $self->handles;
124 return unless $handles;
126 unless ( 'HASH' eq ref $handles ) {
128 "The 'handles' option must be a HASH reference, not $handles");
132 my $to = $handles->{$_};
133 $to = [$to] unless ref $to;
138 around '_make_delegation_method' => sub {
140 my ( $self, $handle_name, $method_to_call ) = @_;
142 my ( $name, @curried_args ) = @$method_to_call;
144 my $accessor_class = $self->_native_accessor_class_for($name);
146 die "Cannot find an accessor class for $name"
147 unless $accessor_class && $accessor_class->can('new');
149 return $accessor_class->new(
150 name => $handle_name,
151 package_name => $self->associated_class->name,
152 delegate_to_method => $name,
155 curried_arguments => \@curried_args,
156 root_types => [ $self->_root_types ],
161 return $_[0]->_helper_type;
164 sub _native_accessor_class_for {
165 my ( $self, $suffix ) = @_;
168 = 'Moose::Meta::Method::Accessor::Native::'
169 . $self->_native_type . '::'
172 Class::MOP::load_class($role);
173 return Moose::Meta::Class->create_anon_class(
175 [ $self->accessor_metaclass, $self->delegation_metaclass ],
181 sub _build_native_type {
184 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
185 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
188 die "Cannot calculate native type for " . ref $self;
191 has '_native_type' => (
195 builder => '_build_native_type',
199 no Moose::Util::TypeConstraints;
207 Moose::Meta::Attribute::Native::Trait - Shared role for native delegation traits
211 See L<Moose/BUGS> for details on reporting bugs.
215 Documentation for Moose native traits can be found in
216 L<Moose::Meta::Attribute::Native>.
226 =head1 COPYRIGHT AND LICENSE
228 Copyright 2007-2009 by Infinity Interactive, Inc.
230 L<http://www.iinteractive.com>
232 This library is free software; you can redistribute it and/or modify
233 it under the same terms as Perl itself.