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 $method_constructors = $self->method_constructors;
47 my %handles = $self->_canonicalize_handles;
49 for my $original_method ( values %handles ) {
50 my $name = $original_method->[0];
52 my $accessor_class = $self->_native_accessor_class_for($name);
55 ( ( $accessor_class && $accessor_class->can('new') )
56 || exists $method_constructors->{$name} )
57 || confess "$name is an unsupported method type";
61 around '_canonicalize_handles' => sub {
64 my $handles = $self->handles;
66 return unless $handles;
68 unless ( 'HASH' eq ref $handles ) {
70 "The 'handles' option must be a HASH reference, not $handles");
74 my $to = $handles->{$_};
75 $to = [$to] unless ref $to;
80 around '_make_delegation_method' => sub {
82 my ( $self, $handle_name, $method_to_call ) = @_;
84 my ( $name, @curried_args ) = @$method_to_call;
86 my $accessor_class = $self->_native_accessor_class_for($name);
89 if ( $accessor_class && $accessor_class->can('new') ) {
90 return $accessor_class->new(
92 package_name => $self->associated_class->name,
94 curried_arguments => \@curried_args,
95 root_types => [ $self->_root_types ],
100 my $method_constructors = $self->method_constructors;
102 my $code = $method_constructors->{$name}->(
104 $self->get_read_method_ref,
105 $self->get_write_method_ref,
112 my $instance = shift;
113 return $code->( $instance, @curried_args, @_ );
120 return $_[0]->_helper_type;
123 sub _native_accessor_class_for {
124 my ( $self, $suffix ) = @_;
127 return unless $self->can('_native_type');
129 return 'Moose::Meta::Method::Accessor::Native::' . $self->_native_type . '::' . $suffix;
132 has 'method_constructors' => (
139 unless $self->can('has_method_provider')
140 && $self->has_method_provider;
142 # or grab them from the role/class
143 my $method_provider = $self->method_provider->meta;
144 return +{ map { $_->name => $_ }
145 $method_provider->_get_local_methods };
150 no Moose::Util::TypeConstraints;
158 Moose::Meta::Attribute::Native::Trait - Base role for helpers
162 See L<Moose/BUGS> for details on reporting bugs.
166 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
176 =head1 COPYRIGHT AND LICENSE
178 Copyright 2007-2009 by Infinity Interactive, Inc.
180 L<http://www.iinteractive.com>
182 This library is free software; you can redistribute it and/or modify
183 it under the same terms as Perl itself.