Moose::Meta::Attribute::Native - Extend your attribute interfaces
package MyClass; use Moose; has 'mapping' => ( traits => [ 'Hash' ], is => 'rw', isa => 'HashRef[Str]', default => sub { {} }, handles => { exists_in_mapping => 'exists', ids_in_mapping => 'keys', get_mapping => 'get', set_mapping => 'set', set_quantity => [ set => 'quantity' ], }, ); # ... my $obj = MyClass->new; $obj->set_quantity(10); # quantity => 10 $obj->set_mapping(4, 'foo'); # 4 => 'foo' $obj->set_mapping(5, 'bar'); # 5 => 'bar' $obj->set_mapping(6, 'baz'); # 6 => 'baz' # prints 'bar' print $obj->get_mapping(5) if $obj->exists_in_mapping(5); # prints '4, 5, 6' print join ', ', $obj->ids_in_mapping;
While Moose attributes provide a way to name your accessors, readers, writers, clearers and predicates, this library provides commonly used attribute helper methods for more specific types of data.
As seen in the "SYNOPSIS", you specify the data structure via the trait parameter. Available meta classes are below; see "METHOD PROVIDERS".
trait
This module used to exist as the MooseX::AttributeHelpers extension. It was very commonly used, so we moved it into core Moose. Since this gave us a chance to change the interface, you will have to change your code or continue using the MooseX::AttributeHelpers extension.
This is like handles in "has" in Moose, but only HASH references are allowed. Keys are method names that you want installed locally, and values are methods from the method providers (below). Currying with delegated methods works normally for handles.
handles
Common numerical operations.
has 'integer' => ( traits => ['Number'], is => 'ro', isa => 'Int', default => 5, handles => { set => 'set', add => 'add', sub => 'sub', mul => 'mul', div => 'div', mod => 'mod', abs => 'abs', } );
Common methods for string operations.
has 'text' => ( traits => ['String'], is => 'rw', isa => 'Str', default => q{}, handles => { add_text => 'append', replace_text => 'replace', } );
Methods for incrementing and decrementing a counter attribute.
has 'counter' => ( traits => ['Counter'], is => 'ro', isa => 'Num', default => 0, handles => { inc_counter => 'inc', dec_counter => 'dec', reset_counter => 'reset', } );
Common methods for boolean values.
has 'is_lit' => ( traits => ['Bool'], is => 'rw', isa => 'Bool', default => 0, handles => { illuminate => 'set', darken => 'unset', flip_switch => 'toggle', is_dark => 'not', } );
Common methods for hash references.
has 'options' => ( traits => ['Hash'], is => 'ro', isa => 'HashRef[Str]', default => sub { {} }, handles => { set_option => 'set', get_option => 'get', has_option => 'exists', } );
Common methods for array references.
has 'queue' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { add_item => 'push', next_item => 'shift', } );
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.
Stevan Little <stevan@iinteractive.com>
with contributions from:
Robert (rlb3) Boone
Paul (frodwith) Driver
Shawn (Sartak) Moore
Chris (perigrin) Prather
Robert (phaylon) Sedlacek
Tom (dec) Lanyon
Yuval Kogman
Jason May
Cory (gphat) Watson
Florian (rafl) Ragwitz
Evan Carroll
Jesse (doy) Luehrs
Copyright 2007-2009 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Moose
CPAN shell
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.