Commit | Line | Data |
abfdffe0 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
a09601ab |
4 | use Test::More tests => 5; |
abfdffe0 |
5 | use lib 't/lib'; |
6 | |
7 | do { |
a09601ab |
8 | # copied from MouseX::AttributeHelpers; |
9 | package MouseX::AttributeHelpers::Trait::Base; |
10 | use Mouse::Role; |
11 | use Mouse::Util::TypeConstraints; |
1b9e472d |
12 | |
a09601ab |
13 | requires 'helper_type'; |
14 | |
15 | # this is the method map you define ... |
16 | has 'provides' => ( |
17 | is => 'ro', |
18 | isa => 'HashRef', |
19 | default => sub {{}} |
20 | ); |
21 | |
22 | has 'curries' => ( |
23 | is => 'ro', |
24 | isa => 'HashRef', |
25 | default => sub {{}} |
26 | ); |
27 | |
28 | # these next two are the possible methods |
29 | # you can use in the 'provides' map. |
30 | |
31 | # provide a Class or Role which we can |
32 | # collect the method providers from |
33 | |
34 | # requires_attr 'method_provider' |
35 | |
36 | # or you can provide a HASH ref of anon subs |
37 | # yourself. This will also collect and store |
38 | # the methods from a method_provider as well |
39 | has 'method_constructors' => ( |
40 | is => 'ro', |
41 | isa => 'HashRef', |
42 | lazy => 1, |
43 | default => sub { |
44 | my $self = shift; |
45 | return +{} unless $self->has_method_provider; |
46 | # or grab them from the role/class |
47 | my $method_provider = $self->method_provider->meta; |
48 | return +{ |
49 | map { |
50 | $_ => $method_provider->get_method($_) |
51 | } |
52 | grep { $_ ne 'meta' } $method_provider->get_method_list |
53 | }; |
54 | }, |
55 | ); |
56 | |
57 | # extend the parents stuff to make sure |
58 | # certain bits are now required ... |
59 | #has '+default' => (required => 1); |
60 | #has '+type_constraint' => (required => 1); |
61 | |
62 | ## Methods called prior to instantiation |
63 | |
64 | sub process_options_for_provides { |
65 | my ($self, $options) = @_; |
66 | |
67 | if (my $type = $self->helper_type) { |
68 | (exists $options->{isa}) |
69 | || confess "You must define a type with the $type metaclass"; |
70 | |
71 | my $isa = $options->{isa}; |
abfdffe0 |
72 | |
a09601ab |
73 | unless (blessed($isa) && $isa->isa('Mouse::Meta::TypeConstraint')) { |
74 | $isa = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($isa); |
75 | } |
76 | |
77 | #($isa->is_a_type_of($type)) |
78 | # || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type"; |
79 | } |
80 | } |
81 | |
82 | before '_process_options' => sub { |
83 | my ($self, $name, $options) = @_; |
84 | $self->process_options_for_provides($options, $name); |
85 | }; |
86 | |
87 | ## methods called after instantiation |
88 | |
89 | sub check_provides_values { |
90 | my $self = shift; |
91 | |
92 | my $method_constructors = $self->method_constructors; |
93 | |
94 | foreach my $key (keys %{$self->provides}) { |
95 | (exists $method_constructors->{$key}) |
96 | || confess "$key is an unsupported method type"; |
97 | } |
98 | |
99 | foreach my $key (keys %{$self->curries}) { |
100 | (exists $method_constructors->{$key}) |
101 | || confess "$key is an unsupported method type"; |
102 | } |
103 | } |
104 | |
105 | sub _curry { |
106 | my $self = shift; |
107 | my $code = shift; |
108 | |
109 | my @args = @_; |
110 | return sub { |
111 | my $self = shift; |
112 | $code->($self, @args, @_) |
abfdffe0 |
113 | }; |
a09601ab |
114 | } |
115 | |
116 | sub _curry_sub { |
117 | my $self = shift; |
118 | my $body = shift; |
119 | my $code = shift; |
120 | |
121 | return sub { |
122 | my $self = shift; |
123 | $code->($self, $body, @_) |
124 | }; |
125 | } |
126 | |
127 | after 'install_accessors' => sub { |
128 | my $attr = shift; |
129 | my $class = $attr->associated_class; |
130 | |
131 | # grab the reader and writer methods |
132 | # as well, this will be useful for |
133 | # our method provider constructors |
134 | my $attr_reader = $attr->get_read_method; |
135 | my $attr_writer = $attr->get_write_method; |
136 | |
137 | |
138 | # before we install them, lets |
139 | # make sure they are valid |
140 | $attr->check_provides_values; |
141 | |
142 | my $method_constructors = $attr->method_constructors; |
143 | |
144 | my $class_name = $class->name; |
145 | |
146 | while (my ($constructor, $constructed) = each %{$attr->curries}) { |
147 | my $method_code; |
148 | while (my ($curried_name, $curried_arg) = each(%$constructed)) { |
149 | if ($class->has_method($curried_name)) { |
150 | confess |
151 | "The method ($curried_name) already ". |
152 | "exists in class (" . $class->name . ")"; |
153 | } |
154 | my $body = $method_constructors->{$constructor}->( |
155 | $attr, |
156 | $attr_reader, |
157 | $attr_writer, |
158 | ); |
159 | |
160 | if (ref $curried_arg eq 'ARRAY') { |
161 | $method_code = $attr->_curry($body, @$curried_arg); |
162 | } |
163 | elsif (ref $curried_arg eq 'CODE') { |
164 | $method_code = $attr->_curry_sub($body, $curried_arg); |
165 | } |
166 | else { |
167 | confess "curries parameter must be ref type ARRAY or CODE"; |
168 | } |
169 | |
170 | my $method = MouseX::AttributeHelpers::Meta::Method::Curried->wrap( |
171 | $method_code, |
172 | package_name => $class_name, |
173 | name => $curried_name, |
174 | ); |
175 | |
176 | $attr->associate_method($method); |
177 | $class->add_method($curried_name => $method); |
178 | } |
179 | } |
180 | |
181 | foreach my $key (keys %{$attr->provides}) { |
182 | |
183 | my $method_name = $attr->provides->{$key}; |
184 | |
185 | if ($class->has_method($method_name)) { |
186 | confess "The method ($method_name) already exists in class (" . $class->name . ")"; |
187 | } |
188 | |
189 | my $method = $method_constructors->{$key}->( |
190 | $attr, |
191 | $attr_reader, |
192 | $attr_writer, |
abfdffe0 |
193 | ); |
a09601ab |
194 | |
195 | $class->add_method($method_name => $method); |
abfdffe0 |
196 | } |
abfdffe0 |
197 | }; |
198 | |
a09601ab |
199 | package MouseX::AttributeHelpers::Trait::Number; |
200 | use Mouse::Role; |
201 | |
202 | with 'MouseX::AttributeHelpers::Trait::Base'; |
203 | |
204 | sub helper_type { 'Num' } |
205 | |
206 | has 'method_constructors' => ( |
207 | is => 'ro', |
208 | isa => 'HashRef', |
209 | lazy => 1, |
210 | default => sub { |
211 | return +{ |
212 | set => sub { |
213 | my ($attr, $reader, $writer) = @_; |
214 | return sub { $_[0]->$writer($_[1]) }; |
215 | }, |
216 | add => sub { |
217 | my ($attr, $reader, $writer) = @_; |
218 | return sub { $_[0]->$writer($_[0]->$reader() + $_[1]) }; |
219 | }, |
220 | sub => sub { |
221 | my ($attr, $reader, $writer) = @_; |
222 | return sub { $_[0]->$writer($_[0]->$reader() - $_[1]) }; |
223 | }, |
224 | mul => sub { |
225 | my ($attr, $reader, $writer) = @_; |
226 | return sub { $_[0]->$writer($_[0]->$reader() * $_[1]) }; |
227 | }, |
228 | div => sub { |
229 | my ($attr, $reader, $writer) = @_; |
230 | return sub { $_[0]->$writer($_[0]->$reader() / $_[1]) }; |
231 | }, |
232 | mod => sub { |
233 | my ($attr, $reader, $writer) = @_; |
234 | return sub { $_[0]->$writer($_[0]->$reader() % $_[1]) }; |
235 | }, |
236 | abs => sub { |
237 | my ($attr, $reader, $writer) = @_; |
238 | return sub { $_[0]->$writer(abs($_[0]->$reader()) ) }; |
239 | }, |
240 | } |
241 | } |
242 | ); |
243 | |
244 | package MouseX::AttributeHelpers::Number; |
245 | use Mouse; |
246 | |
247 | extends 'Mouse::Meta::Attribute'; |
248 | with 'MouseX::AttributeHelpers::Trait::Number'; |
249 | |
250 | no Mouse; |
251 | |
252 | # register an alias for 'metaclass' |
253 | package Mouse::Meta::Attribute::Custom::MyNumber; |
abfdffe0 |
254 | sub register_implementation { 'MouseX::AttributeHelpers::Number' } |
255 | |
a09601ab |
256 | # register an alias for 'traits' |
257 | package Mouse::Meta::Attribute::Custom::Trait::MyNumber; |
258 | sub register_implementation { 'MouseX::AttributeHelpers::Trait::Number' } |
abfdffe0 |
259 | |
a09601ab |
260 | package MyClass; |
abfdffe0 |
261 | use Mouse; |
262 | |
263 | has 'i' => ( |
a09601ab |
264 | metaclass => 'MyNumber', |
abfdffe0 |
265 | is => 'rw', |
266 | isa => 'Int', |
267 | provides => { |
a09601ab |
268 | 'add' => 'i_add', |
abfdffe0 |
269 | }, |
270 | ); |
a09601ab |
271 | |
272 | package MyClassWithTraits; |
273 | use Mouse; |
274 | |
275 | has 'ii' => ( |
276 | is => 'rw', |
277 | isa => 'Num', |
278 | provides => { |
279 | sub => 'ii_minus', |
280 | abs => 'ii_abs', |
281 | }, |
282 | |
283 | traits => [qw(MyNumber)], |
284 | ); |
abfdffe0 |
285 | }; |
286 | |
a09601ab |
287 | can_ok 'MyClass', 'i_add'; |
288 | my $k = MyClass->new(i=>3); |
289 | $k->i_add(4); |
abfdffe0 |
290 | is $k->i, 7; |
291 | |
a09601ab |
292 | can_ok 'MyClassWithTraits', qw(ii_minus ii_abs); |
293 | |
294 | $k = MyClassWithTraits->new(ii => 10); |
295 | $k->ii_minus(100); |
296 | is $k->ii, -90; |
297 | is $k->ii_abs, 90; |
298 | |