2 package Moose::Meta::Attribute::Native::Trait;
4 use Moose::Util::TypeConstraints;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 requires '_helper_type';
12 before '_process_options' => sub {
13 my ( $self, $name, $options ) = @_;
15 $self->_check_helper_type( $options, $name );
17 $options->{is} = $self->_default_is
18 if !exists $options->{is} && $self->can('_default_is');
20 $options->{default} = $self->_default_default
21 if !exists $options->{default} && $self->can('_default_default');
24 sub _check_helper_type {
25 my ( $self, $options, $name ) = @_;
27 my $type = $self->_helper_type;
29 $options->{isa} = $type
30 unless exists $options->{isa};
32 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
35 ( $isa->is_a_type_of($type) )
37 "The type constraint for $name must be a subtype of $type but it's a $isa";
40 before 'install_accessors' => sub { (shift)->_check_handles_values };
42 sub _check_handles_values {
45 my %handles = $self->_canonicalize_handles;
47 for my $original_method ( values %handles ) {
48 my $name = $original_method->[0];
50 my $accessor_class = $self->_native_accessor_class_for($name);
52 ( $accessor_class && $accessor_class->can('new') )
54 "$name is an unsupported method type - $accessor_class";
58 around '_canonicalize_handles' => sub {
61 my $handles = $self->handles;
63 return unless $handles;
65 unless ( 'HASH' eq ref $handles ) {
67 "The 'handles' option must be a HASH reference, not $handles");
71 my $to = $handles->{$_};
72 $to = [$to] unless ref $to;
77 around '_make_delegation_method' => sub {
79 my ( $self, $handle_name, $method_to_call ) = @_;
81 my ( $name, @curried_args ) = @$method_to_call;
83 my $accessor_class = $self->_native_accessor_class_for($name);
85 die "Cannot find an accessor class for $name"
86 unless $accessor_class && $accessor_class->can('new');
88 return $accessor_class->new(
90 package_name => $self->associated_class->name,
91 delegate_to_method => $name,
94 curried_arguments => \@curried_args,
95 root_types => [ $self->_root_types ],
100 return $_[0]->_helper_type;
103 sub _native_accessor_class_for {
104 my ( $self, $suffix ) = @_;
107 = 'Moose::Meta::Method::Accessor::Native::'
108 . $self->_native_type . '::'
111 return Moose::Meta::Class->create_anon_class(
113 [ $self->accessor_metaclass, $self->delegation_metaclass ],
119 sub _build_native_type {
122 for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
123 return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
126 die "Cannot calculate native type for " . ref $self;
129 has '_native_type' => (
133 builder => '_build_native_type',
137 no Moose::Util::TypeConstraints;
145 Moose::Meta::Attribute::Native::Trait - Base role for helpers
149 See L<Moose/BUGS> for details on reporting bugs.
153 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
163 =head1 COPYRIGHT AND LICENSE
165 Copyright 2007-2009 by Infinity Interactive, Inc.
167 L<http://www.iinteractive.com>
169 This library is free software; you can redistribute it and/or modify
170 it under the same terms as Perl itself.