Commit | Line | Data |
e3c07b19 |
1 | package Moose::AttributeHelpers::Trait::Number; |
2 | use Moose::Role; |
3 | |
115601b6 |
4 | our $VERSION = '0.85'; |
e3c07b19 |
5 | $VERSION = eval $VERSION; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8 | with 'Moose::AttributeHelpers::Trait::Base'; |
9 | |
2e069f5a |
10 | sub _helper_type { 'Num' } |
e3c07b19 |
11 | |
12 | # NOTE: |
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 |
16 | # - SL |
17 | |
18 | has 'method_constructors' => ( |
19 | is => 'ro', |
20 | isa => 'HashRef', |
21 | lazy => 1, |
22 | default => sub { |
23 | return +{ |
24 | set => sub { |
046c8b5e |
25 | my ( $attr, $reader, $writer ) = @_; |
26 | return sub { $writer->( $_[0], $_[1] ) }; |
e3c07b19 |
27 | }, |
28 | add => sub { |
046c8b5e |
29 | my ( $attr, $reader, $writer ) = @_; |
30 | return sub { $writer->( $_[0], $reader->( $_[0] ) + $_[1] ) }; |
e3c07b19 |
31 | }, |
32 | sub => sub { |
046c8b5e |
33 | my ( $attr, $reader, $writer ) = @_; |
34 | return sub { $writer->( $_[0], $reader->( $_[0] ) - $_[1] ) }; |
e3c07b19 |
35 | }, |
36 | mul => sub { |
046c8b5e |
37 | my ( $attr, $reader, $writer ) = @_; |
38 | return sub { $writer->( $_[0], $reader->( $_[0] ) * $_[1] ) }; |
e3c07b19 |
39 | }, |
40 | div => sub { |
046c8b5e |
41 | my ( $attr, $reader, $writer ) = @_; |
42 | return sub { $writer->( $_[0], $reader->( $_[0] ) / $_[1] ) }; |
e3c07b19 |
43 | }, |
44 | mod => sub { |
046c8b5e |
45 | my ( $attr, $reader, $writer ) = @_; |
46 | return sub { $writer->( $_[0], $reader->( $_[0] ) % $_[1] ) }; |
e3c07b19 |
47 | }, |
48 | abs => sub { |
046c8b5e |
49 | my ( $attr, $reader, $writer ) = @_; |
50 | return sub { $writer->( $_[0], abs( $reader->( $_[0] ) ) ) }; |
e3c07b19 |
51 | }, |
046c8b5e |
52 | }; |
e3c07b19 |
53 | } |
54 | ); |
55 | |
56 | no Moose::Role; |
57 | |
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' } |
62 | |
63 | 1; |
64 | |
65 | =pod |
66 | |
67 | =head1 NAME |
68 | |
69 | Moose::AttributeHelpers::Number |
70 | |
71 | =head1 SYNOPSIS |
72 | |
73 | package Real; |
74 | use Moose; |
75 | use Moose::AttributeHelpers; |
76 | |
77 | has 'integer' => ( |
78 | metaclass => 'Number', |
79 | is => 'ro', |
80 | isa => 'Int', |
2edb73d9 |
81 | default => 5, |
5f3663b2 |
82 | handles => { |
e3c07b19 |
83 | set => 'set', |
84 | add => 'add', |
85 | sub => 'sub', |
86 | mul => 'mul', |
87 | div => 'div', |
88 | mod => 'mod', |
89 | abs => 'abs', |
90 | } |
91 | ); |
92 | |
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); |
96 | |
97 | =head1 DESCRIPTION |
98 | |
99 | This provides a simple numeric attribute, which supports most of the |
100 | basic math operations. |
101 | |
102 | =head1 METHODS |
103 | |
104 | =over 4 |
105 | |
106 | =item B<meta> |
107 | |
e3c07b19 |
108 | =item B<method_constructors> |
109 | |
110 | =back |
111 | |
112 | =head1 PROVIDED METHODS |
113 | |
114 | It is important to note that all those methods do in place |
115 | modification of the value stored in the attribute. |
116 | |
117 | =over 4 |
118 | |
119 | =item I<set ($value)> |
120 | |
121 | Alternate way to set the value. |
122 | |
123 | =item I<add ($value)> |
124 | |
125 | Adds the current value of the attribute to C<$value>. |
126 | |
127 | =item I<sub ($value)> |
128 | |
129 | Subtracts the current value of the attribute to C<$value>. |
130 | |
131 | =item I<mul ($value)> |
132 | |
133 | Multiplies the current value of the attribute to C<$value>. |
134 | |
135 | =item I<div ($value)> |
136 | |
137 | Divides the current value of the attribute to C<$value>. |
138 | |
139 | =item I<mod ($value)> |
140 | |
141 | Modulus the current value of the attribute to C<$value>. |
142 | |
143 | =item I<abs> |
144 | |
145 | Sets the current value of the attribute to its absolute value. |
146 | |
147 | =back |
148 | |
149 | =head1 BUGS |
150 | |
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 |
153 | to cpan-RT. |
154 | |
155 | =head1 AUTHOR |
156 | |
157 | Robert Boone |
158 | |
159 | =head1 COPYRIGHT AND LICENSE |
160 | |
161 | Copyright 2007-2009 by Infinity Interactive, Inc. |
162 | |
163 | L<http://www.iinteractive.com> |
164 | |
165 | This library is free software; you can redistribute it and/or modify |
166 | it under the same terms as Perl itself. |
167 | |
168 | =cut |