bump version to 0.91
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait.pm
1
2 package Moose::Meta::Attribute::Native::Trait;
3 use Moose::Role;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.91';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 requires '_helper_type';
11
12 # these next two are the possible methods you can use in the 'handles'
13 # map.
14
15 # provide a Class or Role which we can collect the method providers
16 # from
17
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' => (
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
37 has '+default'         => ( required => 1 );
38 has '+type_constraint' => ( required => 1 );
39
40 # methods called prior to instantiation
41
42 before '_process_options' => sub {
43     my ( $self, $name, $options ) = @_;
44
45     $self->_check_helper_type( $options, $name );
46
47     $options->{is} = $self->_default_is
48         if ! exists $options->{is} && $self->can('_default_is');
49
50     $options->{default} = $self->_default_default
51         if ! exists $options->{default} && $self->can('_default_default');
52 };
53
54 sub _check_helper_type {
55     my ( $self, $options, $name ) = @_;
56
57     my $type = $self->_helper_type;
58
59     $options->{isa} = $type
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 }
69
70 around '_canonicalize_handles' => sub {
71     my $next    = shift;
72     my $self    = shift;
73     my $handles = $self->handles;
74
75     return unless $handles;
76
77     unless ( 'HASH' eq ref $handles ) {
78         $self->throw_error(
79             "The 'handles' option must be a HASH reference, not $handles" );
80     }
81
82     return map {
83         my $to = $handles->{$_};
84         $to = [$to] unless ref $to;
85         $_ => $to
86     } keys %$handles;
87 };
88
89 # methods called after instantiation
90
91 before 'install_accessors' => sub { (shift)->_check_handles_values };
92
93 sub _check_handles_values {
94     my $self = shift;
95
96     my $method_constructors = $self->method_constructors;
97
98     my %handles = $self->_canonicalize_handles;
99
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";
104     }
105
106 }
107
108 around '_make_delegation_method' => sub {
109     my $next = shift;
110     my ( $self, $handle_name, $method_to_call ) = @_;
111
112     my ( $name, @curried_args ) = @$method_to_call;
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;
127             return $code->( $instance, @curried_args, @_ );
128         },
129     );
130 };
131
132 no Moose::Role;
133 no Moose::Util::TypeConstraints;
134
135 1;
136
137 __END__
138
139 =head1 NAME
140
141 Moose::Meta::Attribute::Native::Trait - Base role for helpers
142
143 =head1 BUGS
144
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
147 to cpan-RT.
148
149 =head1 AUTHORS
150
151 Yuval Kogman
152
153 Shawn M Moore
154
155 Jesse Luehrs
156
157 =head1 COPYRIGHT AND LICENSE
158
159 Copyright 2007-2009 by Infinity Interactive, Inc.
160
161 L<http://www.iinteractive.com>
162
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself.
165
166 =cut