Commit | Line | Data |
c466e58f |
1 | package Moose::Meta::Attribute::Native::Trait::Number; |
e3c07b19 |
2 | use Moose::Role; |
3 | |
c466e58f |
4 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
5 | |
2e069f5a |
6 | sub _helper_type { 'Num' } |
e3c07b19 |
7 | |
e3c07b19 |
8 | no Moose::Role; |
9 | |
e3c07b19 |
10 | 1; |
11 | |
e818d161 |
12 | # ABSTRACT: Helper trait for Num attributes |
13 | |
14 | __END__ |
15 | |
e3c07b19 |
16 | =pod |
17 | |
e3c07b19 |
18 | =head1 SYNOPSIS |
19 | |
20 | package Real; |
21 | use Moose; |
e3c07b19 |
22 | |
23 | has 'integer' => ( |
e132fd56 |
24 | traits => ['Number'], |
25 | is => 'ro', |
26 | isa => 'Num', |
27 | default => 5, |
28 | handles => { |
e3c07b19 |
29 | set => 'set', |
30 | add => 'add', |
31 | sub => 'sub', |
32 | mul => 'mul', |
33 | div => 'div', |
34 | mod => 'mod', |
35 | abs => 'abs', |
9610c1d2 |
36 | }, |
e3c07b19 |
37 | ); |
38 | |
39 | my $real = Real->new(); |
e132fd56 |
40 | $real->add(5); # same as $real->integer($real->integer + 5); |
41 | $real->sub(2); # same as $real->integer($real->integer - 2); |
e3c07b19 |
42 | |
43 | =head1 DESCRIPTION |
44 | |
7795e4de |
45 | This trait provides native delegation methods for numbers. All of the |
46 | operations correspond to arithmetic operations like addition or |
47 | multiplication. |
48 | |
49 | =head1 DEFAULT TYPE |
50 | |
51 | If you don't provide an C<isa> value for your attribute, it will default to |
52 | C<Num>. |
e3c07b19 |
53 | |
e3c07b19 |
54 | =head1 PROVIDED METHODS |
55 | |
7795e4de |
56 | All of these methods modify the attribute's value in place. All methods return |
57 | the new value. |
e3c07b19 |
58 | |
59 | =over 4 |
60 | |
e132fd56 |
61 | =item * B<add($value)> |
e3c07b19 |
62 | |
7795e4de |
63 | Adds the current value of the attribute to C<$value>. |
e3c07b19 |
64 | |
e132fd56 |
65 | =item * B<sub($value)> |
e3c07b19 |
66 | |
7795e4de |
67 | Subtracts C<$value> from the current value of the attribute. |
e3c07b19 |
68 | |
e132fd56 |
69 | =item * B<mul($value)> |
e3c07b19 |
70 | |
7795e4de |
71 | Multiplies the current value of the attribute by C<$value>. |
e3c07b19 |
72 | |
e132fd56 |
73 | =item * B<div($value)> |
e3c07b19 |
74 | |
7795e4de |
75 | Divides the current value of the attribute by C<$value>. |
e3c07b19 |
76 | |
e132fd56 |
77 | =item * B<mod($value)> |
e3c07b19 |
78 | |
7795e4de |
79 | Returns the current value of the attribute modulo C<$value>. |
e3c07b19 |
80 | |
e132fd56 |
81 | =item * B<abs> |
e3c07b19 |
82 | |
7795e4de |
83 | Sets the current value of the attribute to its absolute value. |
ace7cdf9 |
84 | |
85 | =back |
86 | |
e3c07b19 |
87 | =head1 BUGS |
88 | |
d4048ef3 |
89 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
90 | |
e3c07b19 |
91 | =cut |