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 # these next two are the possible methods you can use in the 'handles'
15 # provide a Class or Role which we can collect the method providers
18 # or you can provide a HASH ref of anon subs yourself. This will also
19 # collect and store the methods from a method_provider as well
20 has 'method_constructors' => (
26 return +{} unless $self->has_method_provider;
27 # or grab them from the role/class
28 my $method_provider = $self->method_provider->meta;
31 $_ => $method_provider->get_method($_)
32 } $method_provider->get_method_list
37 has '+default' => ( required => 1 );
38 has '+type_constraint' => ( required => 1 );
40 # methods called prior to instantiation
42 before '_process_options' => sub {
43 my ( $self, $name, $options ) = @_;
45 $self->_check_helper_type( $options, $name );
47 $options->{is} = $self->_default_is
48 if ! exists $options->{is} && $self->can('_default_is');
50 $options->{default} = $self->_default_default
51 if ! exists $options->{default} && $self->can('_default_default');
54 sub _check_helper_type {
55 my ( $self, $options, $name ) = @_;
57 my $type = $self->_helper_type;
59 $options->{isa} = $type
60 unless exists $options->{isa};
62 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
65 ( $isa->is_a_type_of($type) )
67 "The type constraint for $name must be a subtype of $type but it's a $isa";
70 around '_canonicalize_handles' => sub {
73 my $handles = $self->handles;
75 return unless $handles;
77 unless ( 'HASH' eq ref $handles ) {
79 "The 'handles' option must be a HASH reference, not $handles" );
83 my $to = $handles->{$_};
84 $to = [$to] unless ref $to;
89 # methods called after instantiation
91 before 'install_accessors' => sub { (shift)->_check_handles_values };
93 sub _check_handles_values {
96 my $method_constructors = $self->method_constructors;
98 my %handles = $self->_canonicalize_handles;
100 for my $original_method ( values %handles ) {
101 my $name = $original_method->[0];
102 ( exists $method_constructors->{$name} )
103 || confess "$name is an unsupported method type";
108 around '_make_delegation_method' => sub {
110 my ( $self, $handle_name, $method_to_call ) = @_;
112 my ( $name, @curried_args ) = @$method_to_call;
114 my $method_constructors = $self->method_constructors;
116 my $code = $method_constructors->{$name}->(
118 $self->get_read_method_ref,
119 $self->get_write_method_ref,
126 my $instance = shift;
127 return $code->( $instance, @curried_args, @_ );
133 no Moose::Util::TypeConstraints;
141 Moose::Meta::Attribute::Native::Trait - Base role for helpers
145 All complex software has bugs lurking in it, and this module is no
146 exception. If you find a bug please either email me, or add the bug
151 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
161 =head1 COPYRIGHT AND LICENSE
163 Copyright 2007-2009 by Infinity Interactive, Inc.
165 L<http://www.iinteractive.com>
167 This library is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself.