bump version to 0.91
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait.pm
CommitLineData
e3c07b19 1
c466e58f 2package Moose::Meta::Attribute::Native::Trait;
e3c07b19 3use Moose::Role;
4use Moose::Util::TypeConstraints;
5
113d3174 6our $VERSION = '0.91';
e3c07b19 7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
2e069f5a 10requires '_helper_type';
e3c07b19 11
a65d8455 12# these next two are the possible methods you can use in the 'handles'
13# map.
e3c07b19 14
a65d8455 15# provide a Class or Role which we can collect the method providers
16# from
e3c07b19 17
a65d8455 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
e3c07b19 20has 'method_constructors' => (
21 is => 'ro',
22 isa => 'HashRef',
23 lazy => 1,
24 default => sub {
25 my $self = shift;
26 return +{} unless $self->has_method_provider;
27 # or grab them from the role/class
28 my $method_provider = $self->method_provider->meta;
29 return +{
30 map {
31 $_ => $method_provider->get_method($_)
32 } $method_provider->get_method_list
33 };
34 },
35);
36
046c8b5e 37has '+default' => ( required => 1 );
38has '+type_constraint' => ( required => 1 );
e3c07b19 39
a65d8455 40# methods called prior to instantiation
e3c07b19 41
2edb73d9 42before '_process_options' => sub {
43 my ( $self, $name, $options ) = @_;
e3c07b19 44
2edb73d9 45 $self->_check_helper_type( $options, $name );
e3c07b19 46
2edb73d9 47 $options->{is} = $self->_default_is
48 if ! exists $options->{is} && $self->can('_default_is');
e3c07b19 49
2edb73d9 50 $options->{default} = $self->_default_default
51 if ! exists $options->{default} && $self->can('_default_default');
52};
e3c07b19 53
2edb73d9 54sub _check_helper_type {
55 my ( $self, $options, $name ) = @_;
e3c07b19 56
2e069f5a 57 my $type = $self->_helper_type;
2edb73d9 58
2e069f5a 59 $options->{isa} = $type
2edb73d9 60 unless exists $options->{isa};
61
62 my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
63 $options->{isa} );
64
65 ( $isa->is_a_type_of($type) )
66 || confess
67 "The type constraint for $name must be a subtype of $type but it's a $isa";
68}
e3c07b19 69
5404f169 70around '_canonicalize_handles' => sub {
71 my $next = shift;
72 my $self = shift;
73 my $handles = $self->handles;
046c8b5e 74
5404f169 75 return unless $handles;
046c8b5e 76
77 unless ( 'HASH' eq ref $handles ) {
5404f169 78 $self->throw_error(
046c8b5e 79 "The 'handles' option must be a HASH reference, not $handles" );
5404f169 80 }
046c8b5e 81
5404f169 82 return map {
83 my $to = $handles->{$_};
046c8b5e 84 $to = [$to] unless ref $to;
5404f169 85 $_ => $to
86 } keys %$handles;
87};
88
a65d8455 89# methods called after instantiation
e3c07b19 90
2edb73d9 91before 'install_accessors' => sub { (shift)->_check_handles_values };
5404f169 92
2edb73d9 93sub _check_handles_values {
e3c07b19 94 my $self = shift;
95
96 my $method_constructors = $self->method_constructors;
97
5404f169 98 my %handles = $self->_canonicalize_handles;
e3c07b19 99
046c8b5e 100 for my $original_method ( values %handles ) {
5404f169 101 my $name = $original_method->[0];
046c8b5e 102 ( exists $method_constructors->{$name} )
5404f169 103 || confess "$name is an unsupported method type";
e3c07b19 104 }
e3c07b19 105
5404f169 106}
e3c07b19 107
18281451 108around '_make_delegation_method' => sub {
109 my $next = shift;
046c8b5e 110 my ( $self, $handle_name, $method_to_call ) = @_;
18281451 111
3c573ca4 112 my ( $name, @curried_args ) = @$method_to_call;
18281451 113
114 my $method_constructors = $self->method_constructors;
115
116 my $code = $method_constructors->{$name}->(
117 $self,
118 $self->get_read_method_ref,
119 $self->get_write_method_ref,
120 );
121
122 return $next->(
123 $self,
124 $handle_name,
125 sub {
126 my $instance = shift;
3c573ca4 127 return $code->( $instance, @curried_args, @_ );
18281451 128 },
129 );
130};
131
e3c07b19 132no Moose::Role;
133no Moose::Util::TypeConstraints;
134
1351;
136
137__END__
138
139=head1 NAME
140
2420461c 141Moose::Meta::Attribute::Native::Trait - Base role for helpers
e3c07b19 142
e3c07b19 143=head1 BUGS
144
145All complex software has bugs lurking in it, and this module is no
146exception. If you find a bug please either email me, or add the bug
147to cpan-RT.
148
149=head1 AUTHORS
150
151Yuval Kogman
152
153Shawn M Moore
154
155Jesse Luehrs
156
157=head1 COPYRIGHT AND LICENSE
158
159Copyright 2007-2009 by Infinity Interactive, Inc.
160
161L<http://www.iinteractive.com>
162
163This library is free software; you can redistribute it and/or modify
164it under the same terms as Perl itself.
165
166=cut