Add accessor to Collection::Hash
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Hash.pm
CommitLineData
65a43f48 1package MooseX::AttributeHelpers::MethodProvider::Hash;
2use Moose::Role;
3
9e2db1c2 4our $VERSION = '0.17';
38430345 5$VERSION = eval $VERSION;
457dc4fb 6our $AUTHORITY = 'cpan:STEVAN';
7
9a976497 8with 'MooseX::AttributeHelpers::MethodProvider::ImmutableHash';
65a43f48 9
10sub set : method {
457dc4fb 11 my ($attr, $reader, $writer) = @_;
9a976497 12 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
13 my $container_type_constraint = $attr->type_constraint->type_parameter;
65a43f48 14 return sub {
05f7da43 15 my ( $self, @kvp ) = @_;
16
17 my ( @keys, @values );
18
19 while ( @kvp ) {
20 my ( $key, $value ) = ( shift(@kvp), shift(@kvp) );
21 ($container_type_constraint->check($value))
22 || confess "Value " . ($value||'undef') . " did not pass container type constraint";
23 push @keys, $key;
24 push @values, $value;
25 }
26
27 if ( @values > 1 ) {
28 @{ $reader->($self) }{@keys} = @values;
29 } else {
30 $reader->($self)->{$keys[0]} = $values[0];
31 }
65a43f48 32 };
33 }
34 else {
05f7da43 35 return sub {
36 if ( @_ == 3 ) {
37 $reader->($_[0])->{$_[1]} = $_[2]
38 } else {
39 my ( $self, @kvp ) = @_;
40 my ( @keys, @values );
41
42 while ( @kvp ) {
43 push @keys, shift @kvp;
44 push @values, shift @kvp;
45 }
46
37cd0795 47 @{ $reader->($_[0]) }{@keys} = @values;
05f7da43 48 }
49 };
65a43f48 50 }
51}
52
671d0d24 53sub accessor : method {
54 my ($attr, $reader, $writer) = @_;
55
56 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
57 my $container_type_constraint = $attr->type_constraint->type_parameter;
58 return sub {
59 my $self = shift;
60
61 if (@_ == 1) { # reader
62 return $reader->($self)->{$_[0]};
63 }
64 elsif (@_ == 2) { # writer
65 ($container_type_constraint->check($_[1]))
66 || confess "Value " . ($_[1]||'undef') . " did not pass container type constraint";
67 $reader->($self)->{$_[0]} = $_[1];
68 }
69 else {
70 confess "One or two arguments expected, not " . @_;
71 }
72 };
73 }
74 else {
75 return sub {
76 my $self = shift;
77
78 if (@_ == 1) { # reader
79 return $reader->($self)->{$_[0]};
80 }
81 elsif (@_ == 2) { # writer
82 $reader->($self)->{$_[0]} = $_[1];
83 }
84 else {
85 confess "One or two arguments expected, not " . @_;
86 }
87 };
88 }
89}
90
8cf40f80 91sub clear : method {
92 my ($attr, $reader, $writer) = @_;
93 return sub { %{$reader->($_[0])} = () };
94}
95
65a43f48 96sub delete : method {
457dc4fb 97 my ($attr, $reader, $writer) = @_;
d1213e92 98 return sub {
99 my $hashref = $reader->(shift);
100 CORE::delete @{$hashref}{@_};
101 };
65a43f48 102}
103
1041;
105
5431dff2 106__END__
107
108=pod
109
110=head1 NAME
111
112MooseX::AttributeHelpers::MethodProvider::Hash
113
114=head1 DESCRIPTION
115
116This is a role which provides the method generators for
117L<MooseX::AttributeHelpers::Collection::Hash>.
118
9a976497 119This role is composed from the
120L<MooseX::AttributeHelpers::Collection::ImmutableHash> role.
121
5431dff2 122=head1 METHODS
123
124=over 4
125
126=item B<meta>
127
128=back
129
130=head1 PROVIDED METHODS
131
132=over 4
133
134=item B<count>
135
de9d98c6 136Returns the number of elements in the hash.
137
5431dff2 138=item B<delete>
139
de9d98c6 140Removes the element with the given key
141
c0dcad02 142=item B<defined>
143
de9d98c6 144Returns true if the value of a given key is defined
145
5431dff2 146=item B<empty>
147
de9d98c6 148If the list is populated, returns true. Otherwise, returns false.
149
8cf40f80 150=item B<clear>
151
de9d98c6 152Unsets the hash entirely.
153
5431dff2 154=item B<exists>
155
de9d98c6 156Returns true if the given key is present in the hash
157
5431dff2 158=item B<get>
159
de9d98c6 160Returns an element of the hash by its key.
161
5431dff2 162=item B<keys>
163
de9d98c6 164Returns the list of keys in the hash.
165
5431dff2 166=item B<set>
167
de9d98c6 168Sets the element in the hash at the given key to the given value.
169
5431dff2 170=item B<values>
171
de9d98c6 172Returns the list of values in the hash.
173
9a976497 174=item B<kv>
175
de9d98c6 176Returns the key, value pairs in the hash
177
671d0d24 178=item B<accessor>
179
180If passed one argument, returns the value of the requested key. If passed two
181arguments, sets the value of the requested key.
182
5431dff2 183=back
184
185=head1 BUGS
186
187All complex software has bugs lurking in it, and this module is no
188exception. If you find a bug please either email me, or add the bug
189to cpan-RT.
190
191=head1 AUTHOR
192
193Stevan Little E<lt>stevan@iinteractive.comE<gt>
194
195=head1 COPYRIGHT AND LICENSE
196
99c62fb8 197Copyright 2007-2008 by Infinity Interactive, Inc.
5431dff2 198
199L<http://www.iinteractive.com>
200
201This library is free software; you can redistribute it and/or modify
202it under the same terms as Perl itself.
203
204=cut
205