X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FAttribute%2FNative%2FTrait%2FNumber.pm;h=74b7714bd07a6523b99c7b2b18662c78110d76c0;hb=0953b5a4784ae9e36130d87ad9bff255b5c581c8;hp=d64fc1bbbaffe4a9237e6346933c86d176ee6673;hpb=6d0815b59db07f71cdbfd978ed6f574e57e2b3ea;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Number.pm b/lib/Moose/Meta/Attribute/Native/Trait/Number.pm index d64fc1b..74b7714 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Number.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Number.pm @@ -1,55 +1,22 @@ package Moose::Meta::Attribute::Native::Trait::Number; use Moose::Role; -our $VERSION = '0.93'; +our $VERSION = '1.15'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; +use Moose::Meta::Method::Accessor::Native::Number::abs; +use Moose::Meta::Method::Accessor::Native::Number::add; +use Moose::Meta::Method::Accessor::Native::Number::div; +use Moose::Meta::Method::Accessor::Native::Number::mod; +use Moose::Meta::Method::Accessor::Native::Number::mul; +use Moose::Meta::Method::Accessor::Native::Number::set; +use Moose::Meta::Method::Accessor::Native::Number::sub; + with 'Moose::Meta::Attribute::Native::Trait'; sub _helper_type { 'Num' } -# NOTE: we don't use the method provider for this module since many of -# the names of the provided methods would conflict with keywords - SL - -has 'method_constructors' => ( - is => 'ro', - isa => 'HashRef', - lazy => 1, - default => sub { - return +{ - set => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], $_[1] ) }; - }, - add => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], $reader->( $_[0] ) + $_[1] ) }; - }, - sub => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], $reader->( $_[0] ) - $_[1] ) }; - }, - mul => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], $reader->( $_[0] ) * $_[1] ) }; - }, - div => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], $reader->( $_[0] ) / $_[1] ) }; - }, - mod => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], $reader->( $_[0] ) % $_[1] ) }; - }, - abs => sub { - my ( $attr, $reader, $writer ) = @_; - return sub { $writer->( $_[0], abs( $reader->( $_[0] ) ) ) }; - }, - }; - } -); - no Moose::Role; 1; @@ -66,11 +33,11 @@ Moose::Meta::Attribute::Native::Trait::Number - Helper trait for Num attributes use Moose; has 'integer' => ( - traits => ['Number'], - is => 'ro', - isa => 'Num', - default => 5, - handles => { + traits => ['Number'], + is => 'ro', + isa => 'Num', + default => 5, + handles => { set => 'set', add => 'add', sub => 'sub', @@ -82,67 +49,56 @@ Moose::Meta::Attribute::Native::Trait::Number - Helper trait for Num attributes ); my $real = Real->new(); - $real->add(5); # same as $real->integer($real->integer + 5); - $real->sub(2); # same as $real->integer($real->integer - 2); + $real->add(5); # same as $real->integer($real->integer + 5); + $real->sub(2); # same as $real->integer($real->integer - 2); =head1 DESCRIPTION -This provides a simple numeric attribute, which supports most of the -basic math operations. +This trait provides native delegation methods for numbers. All of the +operations correspond to arithmetic operations like addition or +multiplication. -=head1 PROVIDED METHODS +=head1 DEFAULT TYPE -It is important to note that all those methods do in place modification of the -value stored in the attribute. These methods are implemented within this -package. +If you don't provide an C value for your attribute, it will default to +C. -=over 4 +=head1 PROVIDED METHODS -=item B +All of these methods modify the attribute's value in place. All methods return +the new value. -Alternate way to set the value. +=over 4 -=item B +=item * B Adds the current value of the attribute to C<$value>. -=item B +=item * B Subtracts C<$value> from the current value of the attribute. -=item B +=item * B Multiplies the current value of the attribute by C<$value>. -=item B +=item * B Divides the current value of the attribute by C<$value>. -=item B +=item * B Returns the current value of the attribute modulo C<$value>. -=item B +=item * B Sets the current value of the attribute to its absolute value. =back -=head1 METHODS - -=over 4 - -=item B - -=item B - -=back - =head1 BUGS -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +See L for details on reporting bugs. =head1 AUTHOR