1 package MooseX::AttributeHelpers::Trait::Number;
5 our $AUTHORITY = 'cpan:STEVAN';
7 with 'MooseX::AttributeHelpers::Trait::Base';
9 sub helper_type { 'Num' }
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
17 has 'method_constructors' => (
24 my ($attr, $reader, $writer) = @_;
25 return sub { $writer->($_[0], $_[1]) };
28 my ($attr, $reader, $writer) = @_;
29 return sub { $writer->($_[0], $reader->($_[0]) + $_[1]) };
32 my ($attr, $reader, $writer) = @_;
33 return sub { $writer->($_[0], $reader->($_[0]) - $_[1]) };
36 my ($attr, $reader, $writer) = @_;
37 return sub { $writer->($_[0], $reader->($_[0]) * $_[1]) };
40 my ($attr, $reader, $writer) = @_;
41 return sub { $writer->($_[0], $reader->($_[0]) / $_[1]) };
44 my ($attr, $reader, $writer) = @_;
45 return sub { $writer->($_[0], $reader->($_[0]) % $_[1]) };
48 my ($attr, $reader, $writer) = @_;
49 return sub { $writer->($_[0], abs($reader->($_[0])) ) };
57 # register the alias ...
58 package # hide me from search.cpan.org
59 Moose::Meta::Attribute::Custom::Trait::Number;
60 sub register_implementation { 'MooseX::AttributeHelpers::Trait::Number' }
68 MooseX::AttributeHelpers::Number
74 use MooseX::AttributeHelpers;
77 metaclass => 'Number',
92 my $real = Real->new();
93 $real->add(5); # same as $real->integer($real->integer + 5);
94 $real->sub(2); # same as $real->integer($real->integer - 2);
98 This provides a simple numeric attribute, which supports most of the
99 basic math operations.
109 =item B<method_constructors>
113 =head1 PROVIDED METHODS
115 It is important to note that all those methods do in place
116 modification of the value stored in the attribute.
120 =item I<set ($value)>
122 Alternate way to set the value.
124 =item I<add ($value)>
126 Adds the current value of the attribute to C<$value>.
128 =item I<sub ($value)>
130 Subtracts the current value of the attribute to C<$value>.
132 =item I<mul ($value)>
134 Multiplies the current value of the attribute to C<$value>.
136 =item I<div ($value)>
138 Divides the current value of the attribute to C<$value>.
140 =item I<mod ($value)>
142 Modulus the current value of the attribute to C<$value>.
146 Sets the current value of the attribute to its absolute value.
152 All complex software has bugs lurking in it, and this module is no
153 exception. If you find a bug please either email me, or add the bug
160 =head1 COPYRIGHT AND LICENSE
162 Copyright 2007-2008 by Infinity Interactive, Inc.
164 L<http://www.iinteractive.com>
166 This library is free software; you can redistribute it and/or modify
167 it under the same terms as Perl itself.