now uses faster methods for accessors and some other minor cleanup stuff
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Number.pm
CommitLineData
565fe238 1package MooseX::AttributeHelpers::Number;
2use Moose;
565fe238 3
4our $VERSION = '0.01';
5our $AUTHORITY = 'cpan:STEVAN';
6
7extends 'MooseX::AttributeHelpers::Base';
8
9sub helper_type { 'Num' }
10
457dc4fb 11# NOTE:
12# we don't use the method provider for this
13# module since many of the names of the provied
14# methods would conflict with keywords
15# - SL
16
565fe238 17has '+method_constructors' => (
18 default => sub {
19 return +{
20 set => sub {
457dc4fb 21 my ($attr, $reader, $writer) = @_;
22 return sub { $writer->($_[0], $_[1]) };
565fe238 23 },
24 add => sub {
457dc4fb 25 my ($attr, $reader, $writer) = @_;
26 return sub { $writer->($_[0], $reader->($_[0]) + $_[1]) };
565fe238 27 },
28 sub => sub {
457dc4fb 29 my ($attr, $reader, $writer) = @_;
30 return sub { $writer->($_[0], $reader->($_[0]) - $_[1]) };
565fe238 31 },
32 mul => sub {
457dc4fb 33 my ($attr, $reader, $writer) = @_;
34 return sub { $writer->($_[0], $reader->($_[0]) * $_[1]) };
565fe238 35 },
36 div => sub {
457dc4fb 37 my ($attr, $reader, $writer) = @_;
38 return sub { $writer->($_[0], $reader->($_[0]) / $_[1]) };
565fe238 39 },
40 mod => sub {
457dc4fb 41 my ($attr, $reader, $writer) = @_;
42 return sub { $writer->($_[0], $reader->($_[0]) % $_[1]) };
565fe238 43 },
44 abs => sub {
457dc4fb 45 my ($attr, $reader, $writer) = @_;
46 return sub { $writer->($_[0], abs($reader->($_[0])) ) };
565fe238 47 },
48 }
49 }
50);
51
52no Moose;
565fe238 53
54# register the alias ...
55package Moose::Meta::Attribute::Custom::Number;
56sub register_implementation { 'MooseX::AttributeHelpers::Number' }
57
581;
59
60=pod
61
62=head1 NAME
63
64MooseX::AttributeHelpers::Number
65
66=head1 SYNOPSIS
67
68 package Real;
5431dff2 69 use Moose;
70 use MooseX::AttributeHelpers;
71
72 has 'integer' => (
73 metaclass => 'Number',
74 is => 'ro',
75 isa => 'Int',
76 default => sub { 5 },
77 provides => {
78 set => 'set',
79 add => 'add',
80 sub => 'sub',
81 mul => 'mul',
82 div => 'div',
83 mod => 'mod',
84 abs => 'abs',
85 }
86 );
565fe238 87
88 my $real = Real->new();
89 $real->add(5); # same as $real->integer($real->integer + 5);
90 $real->sub(2); # same as $real->integer($real->integer - 2);
91
92=head1 DESCRIPTION
93
5431dff2 94This provides a simple numeric attribute, which supports most of the
95basic math operations.
96
565fe238 97=head1 METHODS
98
5431dff2 99=over 4
100
101=item B<helper_type>
102
103=item B<method_constructors>
104
105=back
106
565fe238 107=head1 BUGS
108
109All complex software has bugs lurking in it, and this module is no
110exception. If you find a bug please either email me, or add the bug
111to cpan-RT.
112
113=head1 AUTHOR
114
8c651099 115Robert Boone
565fe238 116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright 2007 by Infinity Interactive, Inc.
120
121L<http://www.iinteractive.com>
122
123This library is free software; you can redistribute it and/or modify
124it under the same terms as Perl itself.
125
ae2c330e 126=cut