2 package Moose::Meta::Attribute::Native::Trait;
4 use Moose::Util::TypeConstraints;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 requires '_helper_type';
13 before '_process_options' => sub {
14 my ( $self, $name, $options ) = @_;
16 $self->_check_helper_type( $options, $name );
18 if ( !exists $options->{is} && $self->can('_default_is') ) {
19 $options->{is} = $self->_default_is;
21 Moose::Deprecated::deprecated(
22 feature => 'default is for Native Trait',
24 q{Allowing a native trait to automatically supply a value for "is" is deprecated}
28 if ( !exists $options->{default} && $self->can('_default_default') ) {
29 $options->{default} = $self->_default_default;
31 Moose::Deprecated::deprecated(
32 feature => 'default default for Native Trait',
34 'Allowing a native trait to automatically supply a default is deprecated'
39 sub _check_helper_type {
40 my ( $self, $options, $name ) = @_;
42 my $type = $self->_helper_type;
44 $options->{isa} = $type
45 unless exists $options->{isa};
47 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
50 ( $isa->is_a_type_of($type) )
52 "The type constraint for $name must be a subtype of $type but it's a $isa";
55 before 'install_accessors' => sub { (shift)->_check_handles_values };
57 sub _check_handles_values {
60 my %handles = $self->_canonicalize_handles;
62 for my $original_method ( values %handles ) {
63 my $name = $original_method->[0];
65 my $accessor_class = $self->_native_accessor_class_for($name);
67 ( $accessor_class && $accessor_class->can('new') )
69 "$name is an unsupported method type - $accessor_class";
73 around '_canonicalize_handles' => sub {
76 my $handles = $self->handles;
78 return unless $handles;
80 unless ( 'HASH' eq ref $handles ) {
82 "The 'handles' option must be a HASH reference, not $handles");
86 my $to = $handles->{$_};
87 $to = [$to] unless ref $to;
92 around '_make_delegation_method' => sub {
94 my ( $self, $handle_name, $method_to_call ) = @_;
96 my ( $name, @curried_args ) = @$method_to_call;
98 my $accessor_class = $self->_native_accessor_class_for($name);
100 die "Cannot find an accessor class for $name"
101 unless $accessor_class && $accessor_class->can('new');
103 return $accessor_class->new(
104 name => $handle_name,
105 package_name => $self->associated_class->name,
106 delegate_to_method => $name,
109 curried_arguments => \@curried_args,
110 root_types => [ $self->_root_types ],
115 return $_[0]->_helper_type;
118 sub _native_accessor_class_for {
119 my ( $self, $suffix ) = @_;
122 = 'Moose::Meta::Method::Accessor::Native::'
123 . $self->_native_type . '::'
126 return Moose::Meta::Class->create_anon_class(
128 [ $self->accessor_metaclass, $self->delegation_metaclass ],
134 sub _build_native_type {
137 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
138 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
141 die "Cannot calculate native type for " . ref $self;
144 has '_native_type' => (
148 builder => '_build_native_type',
152 no Moose::Util::TypeConstraints;
160 Moose::Meta::Attribute::Native::Trait - Base role for helpers
164 See L<Moose/BUGS> for details on reporting bugs.
168 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
178 =head1 COPYRIGHT AND LICENSE
180 Copyright 2007-2009 by Infinity Interactive, Inc.
182 L<http://www.iinteractive.com>
184 This library is free software; you can redistribute it and/or modify
185 it under the same terms as Perl itself.