Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait; |
e3c07b19 |
3 | use Moose::Role; |
4 | use Moose::Util::TypeConstraints; |
5 | |
9039e8ec |
6 | our $VERSION = '0.89_01'; |
e3c07b19 |
7 | $VERSION = eval $VERSION; |
8 | our $AUTHORITY = 'cpan:STEVAN'; |
9 | |
2e069f5a |
10 | requires '_helper_type'; |
e3c07b19 |
11 | |
a65d8455 |
12 | # these next two are the possible methods you can use in the 'handles' |
13 | # map. |
e3c07b19 |
14 | |
a65d8455 |
15 | # provide a Class or Role which we can collect the method providers |
16 | # from |
e3c07b19 |
17 | |
a65d8455 |
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 |
e3c07b19 |
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 | # or grab them from the role/class |
28 | my $method_provider = $self->method_provider->meta; |
29 | return +{ |
30 | map { |
31 | $_ => $method_provider->get_method($_) |
32 | } $method_provider->get_method_list |
33 | }; |
34 | }, |
35 | ); |
36 | |
046c8b5e |
37 | has '+default' => ( required => 1 ); |
38 | has '+type_constraint' => ( required => 1 ); |
e3c07b19 |
39 | |
a65d8455 |
40 | # methods called prior to instantiation |
e3c07b19 |
41 | |
2edb73d9 |
42 | before '_process_options' => sub { |
43 | my ( $self, $name, $options ) = @_; |
e3c07b19 |
44 | |
2edb73d9 |
45 | $self->_check_helper_type( $options, $name ); |
e3c07b19 |
46 | |
2edb73d9 |
47 | $options->{is} = $self->_default_is |
48 | if ! exists $options->{is} && $self->can('_default_is'); |
e3c07b19 |
49 | |
2edb73d9 |
50 | $options->{default} = $self->_default_default |
51 | if ! exists $options->{default} && $self->can('_default_default'); |
52 | }; |
e3c07b19 |
53 | |
2edb73d9 |
54 | sub _check_helper_type { |
55 | my ( $self, $options, $name ) = @_; |
e3c07b19 |
56 | |
2e069f5a |
57 | my $type = $self->_helper_type; |
2edb73d9 |
58 | |
2e069f5a |
59 | $options->{isa} = $type |
2edb73d9 |
60 | unless exists $options->{isa}; |
61 | |
62 | my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint( |
63 | $options->{isa} ); |
64 | |
65 | ( $isa->is_a_type_of($type) ) |
66 | || confess |
67 | "The type constraint for $name must be a subtype of $type but it's a $isa"; |
68 | } |
e3c07b19 |
69 | |
5404f169 |
70 | around '_canonicalize_handles' => sub { |
71 | my $next = shift; |
72 | my $self = shift; |
73 | my $handles = $self->handles; |
046c8b5e |
74 | |
5404f169 |
75 | return unless $handles; |
046c8b5e |
76 | |
77 | unless ( 'HASH' eq ref $handles ) { |
5404f169 |
78 | $self->throw_error( |
046c8b5e |
79 | "The 'handles' option must be a HASH reference, not $handles" ); |
5404f169 |
80 | } |
046c8b5e |
81 | |
5404f169 |
82 | return map { |
83 | my $to = $handles->{$_}; |
046c8b5e |
84 | $to = [$to] unless ref $to; |
5404f169 |
85 | $_ => $to |
86 | } keys %$handles; |
87 | }; |
88 | |
a65d8455 |
89 | # methods called after instantiation |
e3c07b19 |
90 | |
2edb73d9 |
91 | before 'install_accessors' => sub { (shift)->_check_handles_values }; |
5404f169 |
92 | |
2edb73d9 |
93 | sub _check_handles_values { |
e3c07b19 |
94 | my $self = shift; |
95 | |
96 | my $method_constructors = $self->method_constructors; |
97 | |
5404f169 |
98 | my %handles = $self->_canonicalize_handles; |
e3c07b19 |
99 | |
046c8b5e |
100 | for my $original_method ( values %handles ) { |
5404f169 |
101 | my $name = $original_method->[0]; |
046c8b5e |
102 | ( exists $method_constructors->{$name} ) |
5404f169 |
103 | || confess "$name is an unsupported method type"; |
e3c07b19 |
104 | } |
e3c07b19 |
105 | |
5404f169 |
106 | } |
e3c07b19 |
107 | |
18281451 |
108 | around '_make_delegation_method' => sub { |
109 | my $next = shift; |
046c8b5e |
110 | my ( $self, $handle_name, $method_to_call ) = @_; |
18281451 |
111 | |
3c573ca4 |
112 | my ( $name, @curried_args ) = @$method_to_call; |
18281451 |
113 | |
114 | my $method_constructors = $self->method_constructors; |
115 | |
116 | my $code = $method_constructors->{$name}->( |
117 | $self, |
118 | $self->get_read_method_ref, |
119 | $self->get_write_method_ref, |
120 | ); |
121 | |
122 | return $next->( |
123 | $self, |
124 | $handle_name, |
125 | sub { |
126 | my $instance = shift; |
3c573ca4 |
127 | return $code->( $instance, @curried_args, @_ ); |
18281451 |
128 | }, |
129 | ); |
130 | }; |
131 | |
e3c07b19 |
132 | no Moose::Role; |
133 | no Moose::Util::TypeConstraints; |
134 | |
135 | 1; |
136 | |
137 | __END__ |
138 | |
139 | =head1 NAME |
140 | |
c466e58f |
141 | Moose::Meta::Attribute::Native::Trait - base role for helpers |
e3c07b19 |
142 | |
e3c07b19 |
143 | =head1 BUGS |
144 | |
145 | All complex software has bugs lurking in it, and this module is no |
146 | exception. If you find a bug please either email me, or add the bug |
147 | to cpan-RT. |
148 | |
149 | =head1 AUTHORS |
150 | |
151 | Yuval Kogman |
152 | |
153 | Shawn M Moore |
154 | |
155 | Jesse Luehrs |
156 | |
157 | =head1 COPYRIGHT AND LICENSE |
158 | |
159 | Copyright 2007-2009 by Infinity Interactive, Inc. |
160 | |
161 | L<http://www.iinteractive.com> |
162 | |
163 | This library is free software; you can redistribute it and/or modify |
164 | it under the same terms as Perl itself. |
165 | |
166 | =cut |