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( |
89 | name => $handle_name, |
90 | package_name => $self->associated_class->name, |
91 | attribute => $self, |
54e259f6 |
92 | is_inline => 1, |
78aee58f |
93 | curried_arguments => \@curried_args, |
94 | root_types => [ $self->_root_types ], |
95 | ); |
18281451 |
96 | }; |
97 | |
a6ae7438 |
98 | sub _root_types { |
99 | return $_[0]->_helper_type; |
100 | } |
101 | |
ffc2e25f |
102 | sub _native_accessor_class_for { |
103 | my ( $self, $suffix ) = @_; |
104 | |
ffc2e25f |
105 | return 'Moose::Meta::Method::Accessor::Native::' . $self->_native_type . '::' . $suffix; |
f7fd22b6 |
106 | } |
107 | |
15715245 |
108 | sub _build_native_type { |
109 | my $self = shift; |
110 | |
111 | for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) { |
112 | return $1 if $role_name =~ /::Native::Trait::(\w+)$/; |
113 | } |
114 | |
115 | die "Cannot calculate native type for " . ref $self; |
116 | } |
117 | |
118 | has '_native_type' => ( |
119 | is => 'ro', |
120 | isa => 'Str', |
121 | lazy => 1, |
122 | builder => '_build_native_type', |
123 | ); |
124 | |
e3c07b19 |
125 | no Moose::Role; |
126 | no Moose::Util::TypeConstraints; |
127 | |
128 | 1; |
129 | |
130 | __END__ |
131 | |
132 | =head1 NAME |
133 | |
2420461c |
134 | Moose::Meta::Attribute::Native::Trait - Base role for helpers |
e3c07b19 |
135 | |
e3c07b19 |
136 | =head1 BUGS |
137 | |
d4048ef3 |
138 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
139 | |
1af5d116 |
140 | =head1 SEE ALSO |
141 | |
142 | Documentation for Moose native traits starts at L<Moose::Meta::Attribute Native> |
143 | |
e3c07b19 |
144 | =head1 AUTHORS |
145 | |
146 | Yuval Kogman |
147 | |
148 | Shawn M Moore |
149 | |
150 | Jesse Luehrs |
151 | |
152 | =head1 COPYRIGHT AND LICENSE |
153 | |
154 | Copyright 2007-2009 by Infinity Interactive, Inc. |
155 | |
156 | L<http://www.iinteractive.com> |
157 | |
158 | This library is free software; you can redistribute it and/or modify |
159 | it under the same terms as Perl itself. |
160 | |
161 | =cut |