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