2 package Moose::AttributeHelpers;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
10 use Moose::AttributeHelpers::Meta::Method::Provided;
11 use Moose::AttributeHelpers::Meta::Method::Curried;
13 use Moose::AttributeHelpers::Trait::Bool;
14 use Moose::AttributeHelpers::Trait::Counter;
15 use Moose::AttributeHelpers::Trait::Number;
16 use Moose::AttributeHelpers::Trait::String;
17 use Moose::AttributeHelpers::Trait::Collection::List;
18 use Moose::AttributeHelpers::Trait::Collection::Array;
19 use Moose::AttributeHelpers::Trait::Collection::Hash;
20 use Moose::AttributeHelpers::Trait::Collection::ImmutableHash;
21 use Moose::AttributeHelpers::Trait::Collection::Bag;
23 use Moose::AttributeHelpers::Counter;
24 use Moose::AttributeHelpers::Number;
25 use Moose::AttributeHelpers::String;
26 use Moose::AttributeHelpers::Bool;
27 use Moose::AttributeHelpers::Collection::List;
28 use Moose::AttributeHelpers::Collection::Array;
29 use Moose::AttributeHelpers::Collection::Hash;
30 use Moose::AttributeHelpers::Collection::ImmutableHash;
31 use Moose::AttributeHelpers::Collection::Bag;
41 Moose::AttributeHelpers - Extend your attribute interfaces
47 use Moose::AttributeHelpers;
50 metaclass => 'Collection::Hash',
52 isa => 'HashRef[Str]',
53 default => sub { {} },
55 exists_in_mapping => 'exists',
56 ids_in_mapping => 'keys',
59 set_quantity => [ set => [ 'quantity' ] ],
66 my $obj = MyClass->new;
67 $obj->set_quantity(10); # quantity => 10
68 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
69 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
70 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
74 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
77 print join ', ', $obj->ids_in_mapping;
81 While L<Moose> attributes provide you with a way to name your accessors,
82 readers, writers, clearers and predicates, this library provides commonly
83 used attribute helper methods for more specific types of data.
85 As seen in the L</SYNOPSIS>, you specify the extension via the
86 C<metaclass> parameter. Available meta classes are:
92 This points to a hashref that uses C<method> for the keys and
93 C<stuff> for the values. The method will be added to
94 the object itself and do what you want.
98 This points to a hashref that uses C<provider> for the keys and
99 has two choices for the value:
101 You can supply C<< {method => [ @args ]} >> for the values. The method will be
102 added to the object itself (always using C<@args> as the beginning arguments).
104 Another approach to curry a method provider is to supply a coderef instead of an
105 arrayref. The code ref takes C<$self>, C<$body>, and any additional arguments
106 passed to the final method.
112 times_with_day => sub {
113 my ($self, $body, $datetime) = @_;
114 $body->($self, sub { $_->ymd eq $datetime->ymd });
121 $obj->times_with_day(DateTime->now); # takes datetime argument, checks day
124 =head1 METHOD PROVIDERS
128 =item L<Number|Moose::AttributeHelpers::Number>
130 Common numerical operations.
132 =item L<String|Moose::AttributeHelpers::String>
134 Common methods for string operations.
136 =item L<Counter|Moose::AttributeHelpers::Counter>
138 Methods for incrementing and decrementing a counter attribute.
140 =item L<Bool|Moose::AttributeHelpers::Bool>
142 Common methods for boolean values.
144 =item L<Collection::Hash|Moose::AttributeHelpers::Collection::Hash>
146 Common methods for hash references.
148 =item L<Collection::ImmutableHash|Moose::AttributeHelpers::Collection::ImmutableHash>
150 Common methods for inspecting hash references.
152 =item L<Collection::Array|Moose::AttributeHelpers::Collection::Array>
154 Common methods for array references.
156 =item L<Collection::List|Moose::AttributeHelpers::Collection::List>
158 Common list methods for array references.
164 This is an early release of this module. Right now it is in great need
165 of documentation and tests in the test suite. However, we have used this
166 module to great success at C<$work> where it has been tested very thoroughly
167 and deployed into a major production site.
169 I plan on getting better docs and tests in the next few releases, but until
170 then please refer to the few tests we do have and feel free email and/or
171 message me on irc.perl.org if you have any questions.
175 We need tests and docs badly.
179 All complex software has bugs lurking in it, and this module is no
180 exception. If you find a bug please either email me, or add the bug
185 Stevan Little E<lt>stevan@iinteractive.comE<gt>
187 B<with contributions from:>
191 Paul (frodwith) Driver
195 Chris (perigrin) Prather
197 Robert (phaylon) Sedlacek
207 Florian (rafl) Ragwitz
213 =head1 COPYRIGHT AND LICENSE
215 Copyright 2007-2009 by Infinity Interactive, Inc.
217 L<http://www.iinteractive.com>
219 This library is free software; you can redistribute it and/or modify
220 it under the same terms as Perl itself.