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