mass renaming, including removing MethodProviders from the Trait namespace
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Number.pm
CommitLineData
c466e58f 1package Moose::Meta::Attribute::Native::Trait::Number;
e3c07b19 2use Moose::Role;
3
96539d20 4our $VERSION = '0.87';
e3c07b19 5$VERSION = eval $VERSION;
6our $AUTHORITY = 'cpan:STEVAN';
7
c466e58f 8with 'Moose::Meta::Attribute::Native::Trait';
e3c07b19 9
2e069f5a 10sub _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
15has '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
53no Moose::Role;
54
e3c07b19 551;
56
57=pod
58
59=head1 NAME
60
c466e58f 61Moose::Meta::Attribute::Native::Trait::Number
e3c07b19 62
63=head1 SYNOPSIS
64
65 package Real;
66 use Moose;
67 use Moose::AttributeHelpers;
68
69 has 'integer' => (
70 metaclass => 'Number',
71 is => 'ro',
72 isa => 'Int',
2edb73d9 73 default => 5,
5f3663b2 74 handles => {
e3c07b19 75 set => 'set',
76 add => 'add',
77 sub => 'sub',
78 mul => 'mul',
79 div => 'div',
80 mod => 'mod',
81 abs => 'abs',
82 }
83 );
84
85 my $real = Real->new();
86 $real->add(5); # same as $real->integer($real->integer + 5);
87 $real->sub(2); # same as $real->integer($real->integer - 2);
88
89=head1 DESCRIPTION
90
91This provides a simple numeric attribute, which supports most of the
92basic math operations.
93
94=head1 METHODS
95
96=over 4
97
98=item B<meta>
99
e3c07b19 100=item B<method_constructors>
101
102=back
103
104=head1 PROVIDED METHODS
105
106It is important to note that all those methods do in place
107modification of the value stored in the attribute.
108
109=over 4
110
111=item I<set ($value)>
112
113Alternate way to set the value.
114
115=item I<add ($value)>
116
117Adds the current value of the attribute to C<$value>.
118
119=item I<sub ($value)>
120
121Subtracts the current value of the attribute to C<$value>.
122
123=item I<mul ($value)>
124
125Multiplies the current value of the attribute to C<$value>.
126
127=item I<div ($value)>
128
129Divides the current value of the attribute to C<$value>.
130
131=item I<mod ($value)>
132
133Modulus the current value of the attribute to C<$value>.
134
135=item I<abs>
136
137Sets the current value of the attribute to its absolute value.
138
139=back
140
141=head1 BUGS
142
143All complex software has bugs lurking in it, and this module is no
144exception. If you find a bug please either email me, or add the bug
145to cpan-RT.
146
147=head1 AUTHOR
148
149Robert Boone
150
151=head1 COPYRIGHT AND LICENSE
152
153Copyright 2007-2009 by Infinity Interactive, Inc.
154
155L<http://www.iinteractive.com>
156
157This library is free software; you can redistribute it and/or modify
158it under the same terms as Perl itself.
159
160=cut