2 package Moose::AttributeHelpers;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
10 use Moose::AttributeHelpers::Trait::Bool;
11 use Moose::AttributeHelpers::Trait::Counter;
12 use Moose::AttributeHelpers::Trait::Number;
13 use Moose::AttributeHelpers::Trait::String;
14 use Moose::AttributeHelpers::Trait::Collection::List;
15 use Moose::AttributeHelpers::Trait::Collection::Array;
16 use Moose::AttributeHelpers::Trait::Collection::Hash;
17 use Moose::AttributeHelpers::Trait::Collection::ImmutableHash;
27 Moose::AttributeHelpers - Extend your attribute interfaces
33 use Moose::AttributeHelpers;
36 traits => [ 'Collection::Hash' ],
38 isa => 'HashRef[Str]',
39 default => sub { {} },
41 exists_in_mapping => 'exists',
42 ids_in_mapping => 'keys',
45 set_quantity => [ set => [ 'quantity' ] ],
52 my $obj = MyClass->new;
53 $obj->set_quantity(10); # quantity => 10
54 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
55 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
56 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
60 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
63 print join ', ', $obj->ids_in_mapping;
67 While L<Moose> attributes provide you with a way to name your accessors,
68 readers, writers, clearers and predicates, this library provides commonly
69 used attribute helper methods for more specific types of data.
71 As seen in the L</SYNOPSIS>, you specify the extension via the
72 C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>.
74 This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
75 very commonly used, so we moved it into core Moose. Since this gave us a chance
76 to change the interface, you will have to change your code or continue using
77 the L<MooseX::AttributeHelpers> extension.
83 This is like C<< handles >> in L<Moose/has>, but only HASH references are
84 allowed. Keys are method names that you want installed locally, and values are
85 methods from the method providers (below). Currying with delegated methods
86 works normally for C<< handles >>.
88 =head1 METHOD PROVIDERS
92 =item L<Number|Moose::AttributeHelpers::Trait::Number>
94 Common numerical operations.
96 =item L<String|Moose::AttributeHelpers::Trait::String>
98 Common methods for string operations.
100 =item L<Counter|Moose::AttributeHelpers::Trait::Counter>
102 Methods for incrementing and decrementing a counter attribute.
104 =item L<Bool|Moose::AttributeHelpers::Trait::Bool>
106 Common methods for boolean values.
108 =item L<Collection::Hash|Moose::AttributeHelpers::Trait::Collection::Hash>
110 Common methods for hash references.
112 =item L<Collection::ImmutableHash|Moose::AttributeHelpers::Trait::Collection::ImmutableHash>
114 Common methods for inspecting hash references.
116 =item L<Collection::Array|Moose::AttributeHelpers::Trait::Collection::Array>
118 Common methods for array references.
120 =item L<Collection::List|Moose::AttributeHelpers::Trait::Collection::List>
122 Common list methods for array references.
128 All complex software has bugs lurking in it, and this module is no
129 exception. If you find a bug please either email me, or add the bug
134 Stevan Little E<lt>stevan@iinteractive.comE<gt>
136 B<with contributions from:>
140 Paul (frodwith) Driver
144 Chris (perigrin) Prather
146 Robert (phaylon) Sedlacek
156 Florian (rafl) Ragwitz
162 =head1 COPYRIGHT AND LICENSE
164 Copyright 2007-2009 by Infinity Interactive, Inc.
166 L<http://www.iinteractive.com>
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself.