Commit | Line | Data |
667db0bf |
1 | |
2 | package MooseX::AttributeHelpers::MethodProvider::Number; |
3 | use Moose::Role; |
4 | |
5 | our $VERSION = '0.02'; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8 | my %ops = ( |
9 | add => '+', |
10 | sub => '-', |
11 | mul => '*', |
12 | div => '/', |
13 | mod => '%', |
14 | ); |
720fa35b |
15 | |
667db0bf |
16 | foreach my $method (keys %ops) |
17 | { |
18 | my $s = $ops{$method}; |
19 | __PACKAGE__->meta->alias_method($method, sub { |
20 | my ($attr, $reader, $writer) = @_; |
21 | return eval "sub { \$writer->(\$_[0], \$reader->(\$_[0]) $s \$_[1]) }"; |
22 | }); |
23 | } |
24 | |
25 | sub abs : method { |
26 | my ($attr, $reader, $writer) = @_; |
27 | return sub { $writer->($_[0], CORE::abs($reader->($_[0]))) }; |
28 | } |
29 | |
30 | sub set : method { |
31 | my ($attr, $reader, $writer) = @_; |
32 | return sub { $writer->($_[0], $_[1]) }; |
33 | } |
34 | |
35 | 1; |
36 | |
37 | __END__ |
38 | |
39 | =pod |
40 | |
41 | =head1 NAME |
42 | |
43 | MooseX::AttributeHelpers::MethodProvider::Number |
44 | |
45 | =head1 DESCRIPTION |
46 | |
47 | This is a role which provides the method generators for |
48 | L<MooseX::AttributeHelpers::Number>. |
49 | |
50 | =head1 PROVIDED METHODS |
51 | |
720fa35b |
52 | All methods but 'set' are plain mathematical operators, as in |
53 | C<$current_value = $current_value OP $argument> |
54 | where OP is the operator listed next to the method name. |
667db0bf |
55 | |
56 | =over 4 |
57 | |
720fa35b |
58 | =item B<add> + |
667db0bf |
59 | |
720fa35b |
60 | =item B<sub> - |
667db0bf |
61 | |
720fa35b |
62 | =item B<mul> * |
667db0bf |
63 | |
720fa35b |
64 | =item B<div> / |
667db0bf |
65 | |
720fa35b |
66 | =item B<mod> % |
667db0bf |
67 | |
720fa35b |
68 | =item B<abs> |$val|, or $val = abs($value). |
667db0bf |
69 | |
720fa35b |
70 | =item B<set> |
667db0bf |
71 | |
72 | A way to set the value instead of 'setter' or 'is => "rw"'. This method is |
73 | provided for convenience. |
74 | |
75 | =back |
76 | |
77 | =head1 BUGS |
78 | |
79 | All complex software has bugs lurking in it, and this module is no |
80 | exception. If you find a bug please either email me, or add the bug |
81 | to cpan-RT. |
82 | |
83 | =head1 AUTHOR |
84 | |
85 | Paul Driver E<lt>frowith@cpan.orgE<gt> |
86 | |
87 | =head1 COPYRIGHT AND LICENSE |
88 | |
89 | Copyright 2007-2008 by Infinity Interactive, Inc. |
90 | |
91 | L<http://www.iinteractive.com> |
92 | |
93 | This library is free software; you can redistribute it and/or modify |
94 | it under the same terms as Perl itself. |
95 | |
96 | =cut |