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