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;
18 use Moose::AttributeHelpers::Trait::Collection::Bag;
20 use Moose::AttributeHelpers::Counter;
21 use Moose::AttributeHelpers::Number;
22 use Moose::AttributeHelpers::String;
23 use Moose::AttributeHelpers::Bool;
24 use Moose::AttributeHelpers::Collection::List;
25 use Moose::AttributeHelpers::Collection::Array;
26 use Moose::AttributeHelpers::Collection::Hash;
27 use Moose::AttributeHelpers::Collection::ImmutableHash;
28 use Moose::AttributeHelpers::Collection::Bag;
38 Moose::AttributeHelpers - Extend your attribute interfaces
44 use Moose::AttributeHelpers;
47 metaclass => 'Collection::Hash',
49 isa => 'HashRef[Str]',
50 default => sub { {} },
52 exists_in_mapping => 'exists',
53 ids_in_mapping => 'keys',
56 set_quantity => [ set => [ 'quantity' ] ],
63 my $obj = MyClass->new;
64 $obj->set_quantity(10); # quantity => 10
65 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
66 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
67 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
71 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
74 print join ', ', $obj->ids_in_mapping;
78 While L<Moose> attributes provide you with a way to name your accessors,
79 readers, writers, clearers and predicates, this library provides commonly
80 used attribute helper methods for more specific types of data.
82 As seen in the L</SYNOPSIS>, you specify the extension via the
83 C<metaclass> parameter. Available meta classes are:
89 This points to a hashref that uses C<method> for the keys and
90 C<stuff> for the values. The method will be added to
91 the object itself and do what you want.
95 This points to a hashref that uses C<provider> for the keys and
96 has two choices for the value:
98 You can supply C<< {method => [ @args ]} >> for the values. The method will be
99 added to the object itself (always using C<@args> as the beginning arguments).
101 Another approach to curry a method provider is to supply a coderef instead of an
102 arrayref. The code ref takes C<$self>, C<$body>, and any additional arguments
103 passed to the final method.
109 times_with_day => sub {
110 my ($self, $body, $datetime) = @_;
111 $body->($self, sub { $_->ymd eq $datetime->ymd });
118 $obj->times_with_day(DateTime->now); # takes datetime argument, checks day
121 =head1 METHOD PROVIDERS
125 =item L<Number|Moose::AttributeHelpers::Number>
127 Common numerical operations.
129 =item L<String|Moose::AttributeHelpers::String>
131 Common methods for string operations.
133 =item L<Counter|Moose::AttributeHelpers::Counter>
135 Methods for incrementing and decrementing a counter attribute.
137 =item L<Bool|Moose::AttributeHelpers::Bool>
139 Common methods for boolean values.
141 =item L<Collection::Hash|Moose::AttributeHelpers::Collection::Hash>
143 Common methods for hash references.
145 =item L<Collection::ImmutableHash|Moose::AttributeHelpers::Collection::ImmutableHash>
147 Common methods for inspecting hash references.
149 =item L<Collection::Array|Moose::AttributeHelpers::Collection::Array>
151 Common methods for array references.
153 =item L<Collection::List|Moose::AttributeHelpers::Collection::List>
155 Common list methods for array references.
161 This is an early release of this module. Right now it is in great need
162 of documentation and tests in the test suite. However, we have used this
163 module to great success at C<$work> where it has been tested very thoroughly
164 and deployed into a major production site.
166 I plan on getting better docs and tests in the next few releases, but until
167 then please refer to the few tests we do have and feel free email and/or
168 message me on irc.perl.org if you have any questions.
172 We need tests and docs badly.
176 All complex software has bugs lurking in it, and this module is no
177 exception. If you find a bug please either email me, or add the bug
182 Stevan Little E<lt>stevan@iinteractive.comE<gt>
184 B<with contributions from:>
188 Paul (frodwith) Driver
192 Chris (perigrin) Prather
194 Robert (phaylon) Sedlacek
204 Florian (rafl) Ragwitz
210 =head1 COPYRIGHT AND LICENSE
212 Copyright 2007-2009 by Infinity Interactive, Inc.
214 L<http://www.iinteractive.com>
216 This library is free software; you can redistribute it and/or modify
217 it under the same terms as Perl itself.