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 # methods called prior to instantiation
39 before '_process_options' => sub {
40 my ( $self, $name, $options ) = @_;
42 $self->_check_helper_type( $options, $name );
44 $options->{is} = $self->_default_is
45 if ! exists $options->{is} && $self->can('_default_is');
47 $options->{default} = $self->_default_default
48 if ! exists $options->{default} && $self->can('_default_default');
51 sub _check_helper_type {
52 my ( $self, $options, $name ) = @_;
54 my $type = $self->_helper_type;
56 $options->{isa} = $type
57 unless exists $options->{isa};
59 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
62 ( $isa->is_a_type_of($type) )
64 "The type constraint for $name must be a subtype of $type but it's a $isa";
67 around '_canonicalize_handles' => sub {
70 my $handles = $self->handles;
72 return unless $handles;
74 unless ( 'HASH' eq ref $handles ) {
76 "The 'handles' option must be a HASH reference, not $handles" );
80 my $to = $handles->{$_};
81 $to = [$to] unless ref $to;
86 # methods called after instantiation
88 before 'install_accessors' => sub { (shift)->_check_handles_values };
90 sub _check_handles_values {
93 my $method_constructors = $self->method_constructors;
95 my %handles = $self->_canonicalize_handles;
97 for my $original_method ( values %handles ) {
98 my $name = $original_method->[0];
99 ( exists $method_constructors->{$name} )
100 || confess "$name is an unsupported method type";
105 around '_make_delegation_method' => sub {
107 my ( $self, $handle_name, $method_to_call ) = @_;
109 my ( $name, @curried_args ) = @$method_to_call;
111 my $method_constructors = $self->method_constructors;
113 my $code = $method_constructors->{$name}->(
115 $self->get_read_method_ref,
116 $self->get_write_method_ref,
123 my $instance = shift;
124 return $code->( $instance, @curried_args, @_ );
130 no Moose::Util::TypeConstraints;
138 Moose::Meta::Attribute::Native::Trait - Base role for helpers
142 See L<Moose/BUGS> for details on reporting bugs.
146 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
156 =head1 COPYRIGHT AND LICENSE
158 Copyright 2007-2009 by Infinity Interactive, Inc.
160 L<http://www.iinteractive.com>
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.