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