96996191427daa88d5e0e03a4cbb5c236ee6c391
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Number.pm
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 );
15 foreach my $method (keys %ops)
16 {
17     my $s = $ops{$method};
18     __PACKAGE__->meta->alias_method($method, sub {
19         my ($attr, $reader, $writer) = @_;
20         return eval "sub { \$writer->(\$_[0], \$reader->(\$_[0]) $s \$_[1]) }";
21     });
22 }
23
24 sub abs : method {
25     my ($attr, $reader, $writer) = @_;
26     return sub { $writer->($_[0], CORE::abs($reader->($_[0]))) };
27 }
28
29 sub set : method {
30     my ($attr, $reader, $writer) = @_;
31     return sub { $writer->($_[0], $_[1]) };
32 }
33
34 1;
35
36 __END__
37
38 =pod
39
40 =head1 NAME
41
42 MooseX::AttributeHelpers::MethodProvider::Number
43   
44 =head1 DESCRIPTION
45
46 This is a role which provides the method generators for 
47 L<MooseX::AttributeHelpers::Number>.
48
49 =head1 PROVIDED METHODS
50
51 It is important to note that all those methods do in place modification of the 
52 value stored in the attribute.  All methods but 'set' are plain mathematical
53 operators, as in $current_value = $current_value I<op>$argument, where I<op> is
54 the operator listed next to the method name.
55
56 =over 4
57
58 =item B<add>: +
59
60 =item B<sub>: -
61
62 =item B<mul>: *
63
64 =item B<div>: /
65
66 =item B<mod>: %
67
68 =item B<abs>: |$val|, or $val = abs($value).
69
70 =item B<set>:
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