Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait; |
e3c07b19 |
3 | use Moose::Role; |
4 | use Moose::Util::TypeConstraints; |
5 | |
b6cca0d5 |
6 | our $VERSION = '1.14'; |
e3c07b19 |
7 | $VERSION = eval $VERSION; |
8 | our $AUTHORITY = 'cpan:STEVAN'; |
9 | |
2e069f5a |
10 | requires '_helper_type'; |
e3c07b19 |
11 | |
2edb73d9 |
12 | before '_process_options' => sub { |
13 | my ( $self, $name, $options ) = @_; |
e3c07b19 |
14 | |
2edb73d9 |
15 | $self->_check_helper_type( $options, $name ); |
e3c07b19 |
16 | |
2edb73d9 |
17 | $options->{is} = $self->_default_is |
d4dc38ed |
18 | if !exists $options->{is} && $self->can('_default_is'); |
e3c07b19 |
19 | |
2edb73d9 |
20 | $options->{default} = $self->_default_default |
d4dc38ed |
21 | if !exists $options->{default} && $self->can('_default_default'); |
2edb73d9 |
22 | }; |
e3c07b19 |
23 | |
2edb73d9 |
24 | sub _check_helper_type { |
25 | my ( $self, $options, $name ) = @_; |
e3c07b19 |
26 | |
2e069f5a |
27 | my $type = $self->_helper_type; |
2edb73d9 |
28 | |
2e069f5a |
29 | $options->{isa} = $type |
2edb73d9 |
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 | } |
e3c07b19 |
39 | |
2edb73d9 |
40 | before 'install_accessors' => sub { (shift)->_check_handles_values }; |
5404f169 |
41 | |
2edb73d9 |
42 | sub _check_handles_values { |
e3c07b19 |
43 | my $self = shift; |
44 | |
5404f169 |
45 | my %handles = $self->_canonicalize_handles; |
e3c07b19 |
46 | |
046c8b5e |
47 | for my $original_method ( values %handles ) { |
5404f169 |
48 | my $name = $original_method->[0]; |
f7fd22b6 |
49 | |
ffc2e25f |
50 | my $accessor_class = $self->_native_accessor_class_for($name); |
f7fd22b6 |
51 | |
78aee58f |
52 | ( $accessor_class && $accessor_class->can('new') ) |
53 | || confess |
54 | "$name is an unsupported method type - $accessor_class"; |
e3c07b19 |
55 | } |
5404f169 |
56 | } |
e3c07b19 |
57 | |
d4dc38ed |
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 | |
18281451 |
77 | around '_make_delegation_method' => sub { |
78 | my $next = shift; |
046c8b5e |
79 | my ( $self, $handle_name, $method_to_call ) = @_; |
18281451 |
80 | |
3c573ca4 |
81 | my ( $name, @curried_args ) = @$method_to_call; |
18281451 |
82 | |
ffc2e25f |
83 | my $accessor_class = $self->_native_accessor_class_for($name); |
18281451 |
84 | |
78aee58f |
85 | die "Cannot find an accessor class for $name" |
86 | unless $accessor_class && $accessor_class->can('new'); |
87 | |
88 | return $accessor_class->new( |
8b9641b8 |
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 ], |
78aee58f |
96 | ); |
18281451 |
97 | }; |
98 | |
a6ae7438 |
99 | sub _root_types { |
100 | return $_[0]->_helper_type; |
101 | } |
102 | |
ffc2e25f |
103 | sub _native_accessor_class_for { |
104 | my ( $self, $suffix ) = @_; |
105 | |
8b9641b8 |
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; |
f7fd22b6 |
117 | } |
118 | |
15715245 |
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 | |
e3c07b19 |
136 | no Moose::Role; |
137 | no Moose::Util::TypeConstraints; |
138 | |
139 | 1; |
140 | |
141 | __END__ |
142 | |
143 | =head1 NAME |
144 | |
2420461c |
145 | Moose::Meta::Attribute::Native::Trait - Base role for helpers |
e3c07b19 |
146 | |
e3c07b19 |
147 | =head1 BUGS |
148 | |
d4048ef3 |
149 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
150 | |
1af5d116 |
151 | =head1 SEE ALSO |
152 | |
153 | Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native> |
154 | |
e3c07b19 |
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 |