2 package Moose::Meta::Attribute::Native::Trait;
5 use List::MoreUtils qw( any );
6 use Moose::Util::TypeConstraints;
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 requires '_helper_type';
15 before '_process_options' => sub {
16 my ( $self, $name, $options ) = @_;
18 $self->_check_helper_type( $options, $name );
20 if ( !( any { exists $options->{$_} } qw( is reader writer accessor ) )
21 && $self->can('_default_is') ) {
23 $options->{is} = $self->_default_is;
25 Moose::Deprecated::deprecated(
26 feature => 'default is for Native Trait',
28 q{Allowing a native trait to automatically supply a value for "is" is deprecated}
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'
50 sub _check_helper_type {
51 my ( $self, $options, $name ) = @_;
53 my $type = $self->_helper_type;
55 $options->{isa} = $type
56 unless exists $options->{isa};
58 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
61 ( $isa->is_a_type_of($type) )
63 "The type constraint for $name must be a subtype of $type but it's a $isa";
66 before 'install_accessors' => sub { (shift)->_check_handles_values };
68 sub _check_handles_values {
71 my %handles = $self->_canonicalize_handles;
73 for my $original_method ( values %handles ) {
74 my $name = $original_method->[0];
76 my $accessor_class = $self->_native_accessor_class_for($name);
78 ( $accessor_class && $accessor_class->can('new') )
80 "$name is an unsupported method type - $accessor_class";
84 around '_canonicalize_handles' => sub {
87 my $handles = $self->handles;
89 return unless $handles;
91 unless ( 'HASH' eq ref $handles ) {
93 "The 'handles' option must be a HASH reference, not $handles");
97 my $to = $handles->{$_};
98 $to = [$to] unless ref $to;
103 around '_make_delegation_method' => sub {
105 my ( $self, $handle_name, $method_to_call ) = @_;
107 my ( $name, @curried_args ) = @$method_to_call;
109 my $accessor_class = $self->_native_accessor_class_for($name);
111 die "Cannot find an accessor class for $name"
112 unless $accessor_class && $accessor_class->can('new');
114 return $accessor_class->new(
115 name => $handle_name,
116 package_name => $self->associated_class->name,
117 delegate_to_method => $name,
120 curried_arguments => \@curried_args,
121 root_types => [ $self->_root_types ],
126 return $_[0]->_helper_type;
129 sub _native_accessor_class_for {
130 my ( $self, $suffix ) = @_;
133 = 'Moose::Meta::Method::Accessor::Native::'
134 . $self->_native_type . '::'
137 return Moose::Meta::Class->create_anon_class(
139 [ $self->accessor_metaclass, $self->delegation_metaclass ],
145 sub _build_native_type {
148 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
149 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
152 die "Cannot calculate native type for " . ref $self;
155 has '_native_type' => (
159 builder => '_build_native_type',
163 no Moose::Util::TypeConstraints;
171 Moose::Meta::Attribute::Native::Trait - Shared role for native delegation traits
175 See L<Moose/BUGS> for details on reporting bugs.
179 Documentation for Moose native traits can be found in
180 L<Moose::Meta::Attribute::Native>.
190 =head1 COPYRIGHT AND LICENSE
192 Copyright 2007-2009 by Infinity Interactive, Inc.
194 L<http://www.iinteractive.com>
196 This library is free software; you can redistribute it and/or modify
197 it under the same terms as Perl itself.