documentation fixes
[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
8cf40f80 53sub clear : method {
54 my ($attr, $reader, $writer) = @_;
55 return sub { %{$reader->($_[0])} = () };
56}
57
65a43f48 58sub delete : method {
457dc4fb 59 my ($attr, $reader, $writer) = @_;
d1213e92 60 return sub {
61 my $hashref = $reader->(shift);
62 CORE::delete @{$hashref}{@_};
63 };
65a43f48 64}
65
661;
67
5431dff2 68__END__
69
70=pod
71
72=head1 NAME
73
74MooseX::AttributeHelpers::MethodProvider::Hash
75
76=head1 DESCRIPTION
77
78This is a role which provides the method generators for
79L<MooseX::AttributeHelpers::Collection::Hash>.
80
9a976497 81This role is composed from the
82L<MooseX::AttributeHelpers::Collection::ImmutableHash> role.
83
5431dff2 84=head1 METHODS
85
86=over 4
87
88=item B<meta>
89
90=back
91
92=head1 PROVIDED METHODS
93
94=over 4
95
96=item B<count>
97
de9d98c6 98Returns the number of elements in the hash.
99
5431dff2 100=item B<delete>
101
de9d98c6 102Removes the element with the given key
103
c0dcad02 104=item B<defined>
105
de9d98c6 106Returns true if the value of a given key is defined
107
5431dff2 108=item B<empty>
109
de9d98c6 110If the list is populated, returns true. Otherwise, returns false.
111
8cf40f80 112=item B<clear>
113
de9d98c6 114Unsets the hash entirely.
115
5431dff2 116=item B<exists>
117
de9d98c6 118Returns true if the given key is present in the hash
119
5431dff2 120=item B<get>
121
de9d98c6 122Returns an element of the hash by its key.
123
5431dff2 124=item B<keys>
125
de9d98c6 126Returns the list of keys in the hash.
127
5431dff2 128=item B<set>
129
de9d98c6 130Sets the element in the hash at the given key to the given value.
131
5431dff2 132=item B<values>
133
de9d98c6 134Returns the list of values in the hash.
135
9a976497 136=item B<kv>
137
de9d98c6 138Returns the key, value pairs in the hash
139
5431dff2 140=back
141
142=head1 BUGS
143
144All complex software has bugs lurking in it, and this module is no
145exception. If you find a bug please either email me, or add the bug
146to cpan-RT.
147
148=head1 AUTHOR
149
150Stevan Little E<lt>stevan@iinteractive.comE<gt>
151
152=head1 COPYRIGHT AND LICENSE
153
99c62fb8 154Copyright 2007-2008 by Infinity Interactive, Inc.
5431dff2 155
156L<http://www.iinteractive.com>
157
158This library is free software; you can redistribute it and/or modify
159it under the same terms as Perl itself.
160
161=cut
162