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