All unit tests passing with refactored stuff, documentation updated significantly.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Number.pm
CommitLineData
667db0bf 1
2package MooseX::AttributeHelpers::MethodProvider::Number;
3use Moose::Role;
4
5our $VERSION = '0.02';
6our $AUTHORITY = 'cpan:STEVAN';
7
8my %ops = (
9 add => '+',
10 sub => '-',
11 mul => '*',
12 div => '/',
13 mod => '%',
14);
720fa35b 15
667db0bf 16foreach 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
25sub abs : method {
26 my ($attr, $reader, $writer) = @_;
27 return sub { $writer->($_[0], CORE::abs($reader->($_[0]))) };
28}
29
30sub set : method {
31 my ($attr, $reader, $writer) = @_;
32 return sub { $writer->($_[0], $_[1]) };
33}
34
351;
36
37__END__
38
39=pod
40
41=head1 NAME
42
43MooseX::AttributeHelpers::MethodProvider::Number
44
45=head1 DESCRIPTION
46
47This is a role which provides the method generators for
48L<MooseX::AttributeHelpers::Number>.
49
50=head1 PROVIDED METHODS
51
720fa35b 52All methods but 'set' are plain mathematical operators, as in
53C<$current_value = $current_value OP $argument>
54where 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
72A way to set the value instead of 'setter' or 'is => "rw"'. This method is
73provided for convenience.
74
75=back
76
77=head1 BUGS
78
79All complex software has bugs lurking in it, and this module is no
80exception. If you find a bug please either email me, or add the bug
81to cpan-RT.
82
83=head1 AUTHOR
84
85Paul Driver E<lt>frowith@cpan.orgE<gt>
86
87=head1 COPYRIGHT AND LICENSE
88
89Copyright 2007-2008 by Infinity Interactive, Inc.
90
91L<http://www.iinteractive.com>
92
93This library is free software; you can redistribute it and/or modify
94it under the same terms as Perl itself.
95
96=cut