Bump to 0.12
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Number.pm
CommitLineData
565fe238 1package MooseX::AttributeHelpers::Number;
2use Moose;
565fe238 3
3f58c364 4our $VERSION = '0.12';
565fe238 5our $AUTHORITY = 'cpan:STEVAN';
6
7extends 'MooseX::AttributeHelpers::Base';
8
9sub helper_type { 'Num' }
10
457dc4fb 11# NOTE:
12# we don't use the method provider for this
13# module since many of the names of the provied
14# methods would conflict with keywords
15# - SL
16
565fe238 17has '+method_constructors' => (
18 default => sub {
19 return +{
20 set => sub {
457dc4fb 21 my ($attr, $reader, $writer) = @_;
22 return sub { $writer->($_[0], $_[1]) };
565fe238 23 },
24 add => sub {
457dc4fb 25 my ($attr, $reader, $writer) = @_;
26 return sub { $writer->($_[0], $reader->($_[0]) + $_[1]) };
565fe238 27 },
28 sub => sub {
457dc4fb 29 my ($attr, $reader, $writer) = @_;
30 return sub { $writer->($_[0], $reader->($_[0]) - $_[1]) };
565fe238 31 },
32 mul => sub {
457dc4fb 33 my ($attr, $reader, $writer) = @_;
34 return sub { $writer->($_[0], $reader->($_[0]) * $_[1]) };
565fe238 35 },
36 div => sub {
457dc4fb 37 my ($attr, $reader, $writer) = @_;
38 return sub { $writer->($_[0], $reader->($_[0]) / $_[1]) };
565fe238 39 },
40 mod => sub {
457dc4fb 41 my ($attr, $reader, $writer) = @_;
42 return sub { $writer->($_[0], $reader->($_[0]) % $_[1]) };
565fe238 43 },
44 abs => sub {
457dc4fb 45 my ($attr, $reader, $writer) = @_;
46 return sub { $writer->($_[0], abs($reader->($_[0])) ) };
565fe238 47 },
48 }
49 }
50);
51
52no Moose;
565fe238 53
54# register the alias ...
0f31cc28 55package # hide me from search.cpan.org
56 Moose::Meta::Attribute::Custom::Number;
565fe238 57sub register_implementation { 'MooseX::AttributeHelpers::Number' }
58
591;
60
61=pod
62
63=head1 NAME
64
65MooseX::AttributeHelpers::Number
66
67=head1 SYNOPSIS
68
69 package Real;
5431dff2 70 use Moose;
71 use MooseX::AttributeHelpers;
72
73 has 'integer' => (
74 metaclass => 'Number',
75 is => 'ro',
76 isa => 'Int',
77 default => sub { 5 },
78 provides => {
79 set => 'set',
80 add => 'add',
81 sub => 'sub',
82 mul => 'mul',
83 div => 'div',
84 mod => 'mod',
85 abs => 'abs',
86 }
87 );
565fe238 88
89 my $real = Real->new();
90 $real->add(5); # same as $real->integer($real->integer + 5);
91 $real->sub(2); # same as $real->integer($real->integer - 2);
92
93=head1 DESCRIPTION
94
5431dff2 95This provides a simple numeric attribute, which supports most of the
96basic math operations.
97
565fe238 98=head1 METHODS
99
5431dff2 100=over 4
101
b91f57af 102=item B<meta>
103
5431dff2 104=item B<helper_type>
105
106=item B<method_constructors>
107
108=back
109
c91a1347 110=head1 PROVIDED METHODS
111
112It is important to note that all those methods do in place
113modification of the value stored in the attribute.
114
115=over 4
116
117=item I<set ($value)>
118
119Alternate way to set the value.
120
121=item I<add ($value)>
122
123Adds the current value of the attribute to C<$value>.
124
125=item I<sub ($value)>
126
127Subtracts the current value of the attribute to C<$value>.
128
129=item I<mul ($value)>
130
131Multiplies the current value of the attribute to C<$value>.
132
133=item I<div ($value)>
134
135Divides the current value of the attribute to C<$value>.
136
137=item I<mod ($value)>
138
139Modulus the current value of the attribute to C<$value>.
140
141=item I<abs>
142
143Sets the current value of the attribute to its absolute value.
144
145=back
146
565fe238 147=head1 BUGS
148
149All complex software has bugs lurking in it, and this module is no
150exception. If you find a bug please either email me, or add the bug
151to cpan-RT.
152
153=head1 AUTHOR
154
8c651099 155Robert Boone
565fe238 156
157=head1 COPYRIGHT AND LICENSE
158
99c62fb8 159Copyright 2007-2008 by Infinity Interactive, Inc.
565fe238 160
161L<http://www.iinteractive.com>
162
163This library is free software; you can redistribute it and/or modify
164it under the same terms as Perl itself.
165
ae2c330e 166=cut