No need to specify a Moose version now that we're in core.
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / MethodProvider / Hash.pm
CommitLineData
e3c07b19 1package Moose::AttributeHelpers::MethodProvider::Hash;
2use Moose::Role;
3
37b7c240 4our $VERSION = '0.84';
e3c07b19 5$VERSION = eval $VERSION;
6our $AUTHORITY = 'cpan:STEVAN';
7
8with 'Moose::AttributeHelpers::MethodProvider::ImmutableHash';
9
10sub set : method {
11 my ($attr, $reader, $writer) = @_;
12 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
13 my $container_type_constraint = $attr->type_constraint->type_parameter;
14 return sub {
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 '$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 }
32 };
33 }
34 else {
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
47 @{ $reader->($_[0]) }{@keys} = @values;
48 }
49 };
50 }
51}
52
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 '$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
91sub clear : method {
92 my ($attr, $reader, $writer) = @_;
93 return sub { %{$reader->($_[0])} = () };
94}
95
96sub delete : method {
97 my ($attr, $reader, $writer) = @_;
98 return sub {
99 my $hashref = $reader->(shift);
100 CORE::delete @{$hashref}{@_};
101 };
102}
103
1041;
105
106__END__
107
108=pod
109
110=head1 NAME
111
112Moose::AttributeHelpers::MethodProvider::Hash
113
114=head1 DESCRIPTION
115
116This is a role which provides the method generators for
117L<Moose::AttributeHelpers::Collection::Hash>.
118
119This role is composed from the
120L<Moose::AttributeHelpers::Collection::ImmutableHash> role.
121
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
136Returns the number of elements in the hash.
137
138=item B<delete>
139
140Removes the element with the given key
141
142=item B<defined>
143
144Returns true if the value of a given key is defined
145
146=item B<empty>
147
148If the list is populated, returns true. Otherwise, returns false.
149
150=item B<clear>
151
152Unsets the hash entirely.
153
154=item B<exists>
155
156Returns true if the given key is present in the hash
157
158=item B<get>
159
160Returns an element of the hash by its key.
161
162=item B<keys>
163
164Returns the list of keys in the hash.
165
166=item B<set>
167
168Sets the element in the hash at the given key to the given value.
169
170=item B<values>
171
172Returns the list of values in the hash.
173
174=item B<kv>
175
176Returns the key, value pairs in the hash
177
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
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
197Copyright 2007-2009 by Infinity Interactive, Inc.
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