bump version to 1.15
[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.15';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 requires '_helper_type';
11
12 before '_process_options' => sub {
13     my ( $self, $name, $options ) = @_;
14
15     $self->_check_helper_type( $options, $name );
16
17     $options->{is} = $self->_default_is
18         if !exists $options->{is} && $self->can('_default_is');
19
20     $options->{default} = $self->_default_default
21         if !exists $options->{default} && $self->can('_default_default');
22 };
23
24 sub _check_helper_type {
25     my ( $self, $options, $name ) = @_;
26
27     my $type = $self->_helper_type;
28
29     $options->{isa} = $type
30         unless exists $options->{isa};
31
32     my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
33         $options->{isa} );
34
35     ( $isa->is_a_type_of($type) )
36         || confess
37         "The type constraint for $name must be a subtype of $type but it's a $isa";
38 }
39
40 before 'install_accessors' => sub { (shift)->_check_handles_values };
41
42 sub _check_handles_values {
43     my $self = shift;
44
45     my %handles = $self->_canonicalize_handles;
46
47     for my $original_method ( values %handles ) {
48         my $name = $original_method->[0];
49
50         my $accessor_class = $self->_native_accessor_class_for($name);
51
52         ( $accessor_class && $accessor_class->can('new') )
53             || confess
54             "$name is an unsupported method type - $accessor_class";
55     }
56 }
57
58 around '_canonicalize_handles' => sub {
59     shift;
60     my $self    = shift;
61     my $handles = $self->handles;
62
63     return unless $handles;
64
65     unless ( 'HASH' eq ref $handles ) {
66         $self->throw_error(
67             "The 'handles' option must be a HASH reference, not $handles");
68     }
69
70     return map {
71         my $to = $handles->{$_};
72         $to = [$to] unless ref $to;
73         $_ => $to
74     } keys %$handles;
75 };
76
77 around '_make_delegation_method' => sub {
78     my $next = shift;
79     my ( $self, $handle_name, $method_to_call ) = @_;
80
81     my ( $name, @curried_args ) = @$method_to_call;
82
83     my $accessor_class = $self->_native_accessor_class_for($name);
84
85     die "Cannot find an accessor class for $name"
86         unless $accessor_class && $accessor_class->can('new');
87
88     return $accessor_class->new(
89         name               => $handle_name,
90         package_name       => $self->associated_class->name,
91         delegate_to_method => $name,
92         attribute          => $self,
93         is_inline          => 1,
94         curried_arguments  => \@curried_args,
95         root_types         => [ $self->_root_types ],
96     );
97 };
98
99 sub _root_types {
100     return $_[0]->_helper_type;
101 }
102
103 sub _native_accessor_class_for {
104     my ( $self, $suffix ) = @_;
105
106     my $role
107         = 'Moose::Meta::Method::Accessor::Native::'
108         . $self->_native_type . '::'
109         . $suffix;
110
111     return Moose::Meta::Class->create_anon_class(
112         superclasses =>
113             [ $self->accessor_metaclass, $self->delegation_metaclass ],
114         roles => [$role],
115         cache => 1,
116     )->name;
117 }
118
119 sub _build_native_type {
120     my $self = shift;
121
122     for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
123         return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
124     }
125
126     die "Cannot calculate native type for " . ref $self;
127 }
128
129 has '_native_type' => (
130     is      => 'ro',
131     isa     => 'Str',
132     lazy    => 1,
133     builder => '_build_native_type',
134 );
135
136 no Moose::Role;
137 no Moose::Util::TypeConstraints;
138
139 1;
140
141 __END__
142
143 =head1 NAME
144
145 Moose::Meta::Attribute::Native::Trait - Base role for helpers
146
147 =head1 BUGS
148
149 See L<Moose/BUGS> for details on reporting bugs.
150
151 =head1 SEE ALSO
152
153 Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native>
154
155 =head1 AUTHORS
156
157 Yuval Kogman
158
159 Shawn M Moore
160
161 Jesse Luehrs
162
163 =head1 COPYRIGHT AND LICENSE
164
165 Copyright 2007-2009 by Infinity Interactive, Inc.
166
167 L<http://www.iinteractive.com>
168
169 This library is free software; you can redistribute it and/or modify
170 it under the same terms as Perl itself.
171
172 =cut