Version 0.96.
[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.96';
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 # methods called prior to instantiation
38
39 before '_process_options' => sub {
40     my ( $self, $name, $options ) = @_;
41
42     $self->_check_helper_type( $options, $name );
43
44     $options->{is} = $self->_default_is
45         if ! exists $options->{is} && $self->can('_default_is');
46
47     $options->{default} = $self->_default_default
48         if ! exists $options->{default} && $self->can('_default_default');
49 };
50
51 sub _check_helper_type {
52     my ( $self, $options, $name ) = @_;
53
54     my $type = $self->_helper_type;
55
56     $options->{isa} = $type
57         unless exists $options->{isa};
58
59     my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
60         $options->{isa} );
61
62     ( $isa->is_a_type_of($type) )
63         || confess
64         "The type constraint for $name must be a subtype of $type but it's a $isa";
65 }
66
67 around '_canonicalize_handles' => sub {
68     my $next    = shift;
69     my $self    = shift;
70     my $handles = $self->handles;
71
72     return unless $handles;
73
74     unless ( 'HASH' eq ref $handles ) {
75         $self->throw_error(
76             "The 'handles' option must be a HASH reference, not $handles" );
77     }
78
79     return map {
80         my $to = $handles->{$_};
81         $to = [$to] unless ref $to;
82         $_ => $to
83     } keys %$handles;
84 };
85
86 # methods called after instantiation
87
88 before 'install_accessors' => sub { (shift)->_check_handles_values };
89
90 sub _check_handles_values {
91     my $self = shift;
92
93     my $method_constructors = $self->method_constructors;
94
95     my %handles = $self->_canonicalize_handles;
96
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";
101     }
102
103 }
104
105 around '_make_delegation_method' => sub {
106     my $next = shift;
107     my ( $self, $handle_name, $method_to_call ) = @_;
108
109     my ( $name, @curried_args ) = @$method_to_call;
110
111     my $method_constructors = $self->method_constructors;
112
113     my $code = $method_constructors->{$name}->(
114         $self,
115         $self->get_read_method_ref,
116         $self->get_write_method_ref,
117     );
118
119     return $next->(
120         $self,
121         $handle_name,
122         sub {
123             my $instance = shift;
124             return $code->( $instance, @curried_args, @_ );
125         },
126     );
127 };
128
129 no Moose::Role;
130 no Moose::Util::TypeConstraints;
131
132 1;
133
134 __END__
135
136 =head1 NAME
137
138 Moose::Meta::Attribute::Native::Trait - Base role for helpers
139
140 =head1 BUGS
141
142 See L<Moose/BUGS> for details on reporting bugs.
143
144 =head1 SEE ALSO
145
146 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
147
148 =head1 AUTHORS
149
150 Yuval Kogman
151
152 Shawn M Moore
153
154 Jesse Luehrs
155
156 =head1 COPYRIGHT AND LICENSE
157
158 Copyright 2007-2009 by Infinity Interactive, Inc.
159
160 L<http://www.iinteractive.com>
161
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.
164
165 =cut