2 package Moose::Meta::Attribute::Native::Trait;
5 use List::MoreUtils qw( any uniq );
6 use Moose::Util::TypeConstraints;
9 our $AUTHORITY = 'cpan:STEVAN';
11 requires '_helper_type';
13 has _used_default_is => (
19 before '_process_options' => sub {
20 my ( $self, $name, $options ) = @_;
22 $self->_check_helper_type( $options, $name );
24 if ( !( any { exists $options->{$_} } qw( is reader writer accessor ) )
25 && $self->can('_default_is') ) {
27 $options->{is} = $self->_default_is;
29 $options->{_used_default_is} = 1;
35 || any { exists $options->{$_} } qw( default builder lazy_build )
37 && $self->can('_default_default')
40 $options->{default} = $self->_default_default;
42 Moose::Deprecated::deprecated(
43 feature => 'default default for Native Trait',
45 'Allowing a native trait to automatically supply a default is deprecated.'
46 . ' You can avoid this warning by supplying a default, builder, or making the attribute required'
51 after 'install_accessors' => sub {
54 return unless $self->_used_default_is;
57 = $self->_default_is eq 'rw'
58 ? qw( reader writer accessor )
61 my $name = $self->name;
62 my $class = $self->associated_class->name;
64 for my $meth ( uniq grep {defined} map { $self->$_ } @methods ) {
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};
72 $self->associated_class->add_before_method_modifier(
74 Moose::Deprecated::deprecated(
75 feature => 'default is for Native Trait',
83 sub _check_helper_type {
84 my ( $self, $options, $name ) = @_;
86 my $type = $self->_helper_type;
88 $options->{isa} = $type
89 unless exists $options->{isa};
91 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
94 ( $isa->is_a_type_of($type) )
96 "The type constraint for $name must be a subtype of $type but it's a $isa";
99 before 'install_accessors' => sub { (shift)->_check_handles_values };
101 sub _check_handles_values {
104 my %handles = $self->_canonicalize_handles;
106 for my $original_method ( values %handles ) {
107 my $name = $original_method->[0];
109 my $accessor_class = $self->_native_accessor_class_for($name);
111 ( $accessor_class && $accessor_class->can('new') )
113 "$name is an unsupported method type - $accessor_class";
117 around '_canonicalize_handles' => sub {
120 my $handles = $self->handles;
122 return unless $handles;
124 unless ( 'HASH' eq ref $handles ) {
126 "The 'handles' option must be a HASH reference, not $handles");
130 my $to = $handles->{$_};
131 $to = [$to] unless ref $to;
136 around '_make_delegation_method' => sub {
138 my ( $self, $handle_name, $method_to_call ) = @_;
140 my ( $name, @curried_args ) = @$method_to_call;
142 my $accessor_class = $self->_native_accessor_class_for($name);
144 die "Cannot find an accessor class for $name"
145 unless $accessor_class && $accessor_class->can('new');
147 return $accessor_class->new(
148 name => $handle_name,
149 package_name => $self->associated_class->name,
150 delegate_to_method => $name,
153 curried_arguments => \@curried_args,
154 root_types => [ $self->_root_types ],
159 return $_[0]->_helper_type;
162 sub _native_accessor_class_for {
163 my ( $self, $suffix ) = @_;
166 = 'Moose::Meta::Method::Accessor::Native::'
167 . $self->_native_type . '::'
170 Class::MOP::load_class($role);
171 return Moose::Meta::Class->create_anon_class(
173 [ $self->accessor_metaclass, $self->delegation_metaclass ],
179 sub _build_native_type {
182 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
183 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
186 die "Cannot calculate native type for " . ref $self;
189 has '_native_type' => (
193 builder => '_build_native_type',
197 no Moose::Util::TypeConstraints;
201 # ABSTRACT: Shared role for native delegation traits
207 See L<Moose/BUGS> for details on reporting bugs.
211 Documentation for Moose native traits can be found in
212 L<Moose::Meta::Attribute::Native>.