1 package Moose::AttributeHelpers::Trait::Number;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
8 with 'Moose::AttributeHelpers::Trait::Base';
10 sub _helper_type { 'Num' }
13 # we don't use the method provider for this
14 # module since many of the names of the provied
15 # methods would conflict with keywords
18 has 'method_constructors' => (
25 my ( $attr, $reader, $writer ) = @_;
26 return sub { $writer->( $_[0], $_[1] ) };
29 my ( $attr, $reader, $writer ) = @_;
30 return sub { $writer->( $_[0], $reader->( $_[0] ) + $_[1] ) };
33 my ( $attr, $reader, $writer ) = @_;
34 return sub { $writer->( $_[0], $reader->( $_[0] ) - $_[1] ) };
37 my ( $attr, $reader, $writer ) = @_;
38 return sub { $writer->( $_[0], $reader->( $_[0] ) * $_[1] ) };
41 my ( $attr, $reader, $writer ) = @_;
42 return sub { $writer->( $_[0], $reader->( $_[0] ) / $_[1] ) };
45 my ( $attr, $reader, $writer ) = @_;
46 return sub { $writer->( $_[0], $reader->( $_[0] ) % $_[1] ) };
49 my ( $attr, $reader, $writer ) = @_;
50 return sub { $writer->( $_[0], abs( $reader->( $_[0] ) ) ) };
58 # register the alias ...
59 package # hide me from search.cpan.org
60 Moose::Meta::Attribute::Custom::Trait::Number;
61 sub register_implementation { 'Moose::AttributeHelpers::Trait::Number' }
69 Moose::AttributeHelpers::Number
75 use Moose::AttributeHelpers;
78 metaclass => 'Number',
93 my $real = Real->new();
94 $real->add(5); # same as $real->integer($real->integer + 5);
95 $real->sub(2); # same as $real->integer($real->integer - 2);
99 This provides a simple numeric attribute, which supports most of the
100 basic math operations.
108 =item B<method_constructors>
112 =head1 PROVIDED METHODS
114 It is important to note that all those methods do in place
115 modification of the value stored in the attribute.
119 =item I<set ($value)>
121 Alternate way to set the value.
123 =item I<add ($value)>
125 Adds the current value of the attribute to C<$value>.
127 =item I<sub ($value)>
129 Subtracts the current value of the attribute to C<$value>.
131 =item I<mul ($value)>
133 Multiplies the current value of the attribute to C<$value>.
135 =item I<div ($value)>
137 Divides the current value of the attribute to C<$value>.
139 =item I<mod ($value)>
141 Modulus the current value of the attribute to C<$value>.
145 Sets the current value of the attribute to its absolute value.
151 All complex software has bugs lurking in it, and this module is no
152 exception. If you find a bug please either email me, or add the bug
159 =head1 COPYRIGHT AND LICENSE
161 Copyright 2007-2009 by Infinity Interactive, Inc.
163 L<http://www.iinteractive.com>
165 This library is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself.