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' }
12 # NOTE: we don't use the method provider for this module since many of
13 # the names of the provided methods would conflict with keywords - SL
15 has 'method_constructors' => (
22 my ( $attr, $reader, $writer ) = @_;
23 return sub { $writer->( $_[0], $_[1] ) };
26 my ( $attr, $reader, $writer ) = @_;
27 return sub { $writer->( $_[0], $reader->( $_[0] ) + $_[1] ) };
30 my ( $attr, $reader, $writer ) = @_;
31 return sub { $writer->( $_[0], $reader->( $_[0] ) - $_[1] ) };
34 my ( $attr, $reader, $writer ) = @_;
35 return sub { $writer->( $_[0], $reader->( $_[0] ) * $_[1] ) };
38 my ( $attr, $reader, $writer ) = @_;
39 return sub { $writer->( $_[0], $reader->( $_[0] ) / $_[1] ) };
42 my ( $attr, $reader, $writer ) = @_;
43 return sub { $writer->( $_[0], $reader->( $_[0] ) % $_[1] ) };
46 my ( $attr, $reader, $writer ) = @_;
47 return sub { $writer->( $_[0], abs( $reader->( $_[0] ) ) ) };
55 package # hide me from search.cpan.org
56 Moose::Meta::Attribute::Custom::Trait::Number;
57 sub register_implementation { 'Moose::AttributeHelpers::Trait::Number' }
65 Moose::AttributeHelpers::Number
71 use Moose::AttributeHelpers;
74 metaclass => 'Number',
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);
95 This provides a simple numeric attribute, which supports most of the
96 basic math operations.
104 =item B<method_constructors>
108 =head1 PROVIDED METHODS
110 It is important to note that all those methods do in place
111 modification of the value stored in the attribute.
115 =item I<set ($value)>
117 Alternate way to set the value.
119 =item I<add ($value)>
121 Adds the current value of the attribute to C<$value>.
123 =item I<sub ($value)>
125 Subtracts the current value of the attribute to C<$value>.
127 =item I<mul ($value)>
129 Multiplies the current value of the attribute to C<$value>.
131 =item I<div ($value)>
133 Divides the current value of the attribute to C<$value>.
135 =item I<mod ($value)>
137 Modulus the current value of the attribute to C<$value>.
141 Sets the current value of the attribute to its absolute value.
147 All complex software has bugs lurking in it, and this module is no
148 exception. If you find a bug please either email me, or add the bug
155 =head1 COPYRIGHT AND LICENSE
157 Copyright 2007-2009 by Infinity Interactive, Inc.
159 L<http://www.iinteractive.com>
161 This library is free software; you can redistribute it and/or modify
162 it under the same terms as Perl itself.