2 package Moose::Meta::Attribute::Native::Trait;
5 use List::MoreUtils qw( any uniq );
6 use Moose::Util::TypeConstraints;
9 requires '_helper_type';
11 has _used_default_is => (
17 before '_process_options' => sub {
18 my ( $self, $name, $options ) = @_;
20 $self->_check_helper_type( $options, $name );
22 if ( !( any { exists $options->{$_} } qw( is reader writer accessor ) )
23 && $self->can('_default_is') ) {
25 $options->{is} = $self->_default_is;
27 $options->{_used_default_is} = 1;
33 || any { exists $options->{$_} } qw( default builder lazy_build )
35 && $self->can('_default_default')
38 $options->{default} = $self->_default_default;
40 Moose::Deprecated::deprecated(
41 feature => 'default default for Native Trait',
43 'Allowing a native trait to automatically supply a default is deprecated.'
44 . ' You can avoid this warning by supplying a default, builder, or making the attribute required'
49 after 'install_accessors' => sub {
52 return unless $self->_used_default_is;
55 = $self->_default_is eq 'rw'
56 ? qw( reader writer accessor )
59 my $name = $self->name;
60 my $class = $self->associated_class->name;
62 for my $meth ( uniq grep {defined} map { $self->$_ } @methods ) {
65 = "The $meth method in the $class class was automatically created"
66 . " by the native delegation trait for the $name attribute."
67 . q{ This "default is" feature is deprecated.}
68 . q{ Explicitly set "is" or define accessor names to avoid this};
70 $self->associated_class->add_before_method_modifier(
72 Moose::Deprecated::deprecated(
73 feature => 'default is for Native Trait',
81 sub _check_helper_type {
82 my ( $self, $options, $name ) = @_;
84 my $type = $self->_helper_type;
86 $options->{isa} = $type
87 unless exists $options->{isa};
89 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
92 ( $isa->is_a_type_of($type) )
94 "The type constraint for $name must be a subtype of $type but it's a $isa";
97 before 'install_accessors' => sub { (shift)->_check_handles_values };
99 sub _check_handles_values {
102 my %handles = $self->_canonicalize_handles;
104 for my $original_method ( values %handles ) {
105 my $name = $original_method->[0];
107 my $accessor_class = $self->_native_accessor_class_for($name);
109 ( $accessor_class && $accessor_class->can('new') )
111 "$name is an unsupported method type - $accessor_class";
115 around '_canonicalize_handles' => sub {
118 my $handles = $self->handles;
120 return unless $handles;
122 unless ( 'HASH' eq ref $handles ) {
124 "The 'handles' option must be a HASH reference, not $handles");
128 my $to = $handles->{$_};
129 $to = [$to] unless ref $to;
134 around '_make_delegation_method' => sub {
136 my ( $self, $handle_name, $method_to_call ) = @_;
138 my ( $name, @curried_args ) = @$method_to_call;
140 my $accessor_class = $self->_native_accessor_class_for($name);
142 die "Cannot find an accessor class for $name"
143 unless $accessor_class && $accessor_class->can('new');
145 return $accessor_class->new(
146 name => $handle_name,
147 package_name => $self->associated_class->name,
148 delegate_to_method => $name,
151 curried_arguments => \@curried_args,
152 root_types => [ $self->_root_types ],
157 return $_[0]->_helper_type;
160 sub _native_accessor_class_for {
161 my ( $self, $suffix ) = @_;
164 = 'Moose::Meta::Method::Accessor::Native::'
165 . $self->_native_type . '::'
168 Class::MOP::load_class($role);
169 return Moose::Meta::Class->create_anon_class(
171 [ $self->accessor_metaclass, $self->delegation_metaclass ],
177 sub _build_native_type {
180 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
181 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
184 die "Cannot calculate native type for " . ref $self;
187 has '_native_type' => (
191 builder => '_build_native_type',
195 no Moose::Util::TypeConstraints;
199 # ABSTRACT: Shared role for native delegation traits
205 See L<Moose/BUGS> for details on reporting bugs.
209 Documentation for Moose native traits can be found in
210 L<Moose::Meta::Attribute::Native>.