Composite now implemented.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Hash.pm
CommitLineData
65a43f48 1package MooseX::AttributeHelpers::MethodProvider::Hash;
2use Moose::Role;
720fa35b 3use MooseX::AttributeHelpers::Collection::TypeCheck;
65a43f48 4
532b802c 5our $VERSION = '0.04';
457dc4fb 6our $AUTHORITY = 'cpan:STEVAN';
7
9a976497 8with 'MooseX::AttributeHelpers::MethodProvider::ImmutableHash';
65a43f48 9
10sub set : method {
457dc4fb 11 my ($attr, $reader, $writer) = @_;
720fa35b 12 type_check(
13 $attr,
14 sub {
15 my ($self, %pairs) = @_;
16 return (values %pairs);
17 },
18 sub {
19 my ($self, @pairs) = @_;
20 my $hash = $reader->($self);
21 while (@pairs) {
22 my $key = shift(@pairs);
23 my $value = shift(@pairs);
24 $hash->{$key} = $value;
05f7da43 25 }
720fa35b 26 },
27 );
65a43f48 28}
29
8cf40f80 30sub clear : method {
31 my ($attr, $reader, $writer) = @_;
32 return sub { %{$reader->($_[0])} = () };
33}
34
65a43f48 35sub delete : method {
457dc4fb 36 my ($attr, $reader, $writer) = @_;
d1213e92 37 return sub {
38 my $hashref = $reader->(shift);
39 CORE::delete @{$hashref}{@_};
40 };
65a43f48 41}
42
431;
44
5431dff2 45__END__
46
47=pod
48
49=head1 NAME
50
51MooseX::AttributeHelpers::MethodProvider::Hash
52
53=head1 DESCRIPTION
54
55This is a role which provides the method generators for
720fa35b 56L<MooseX::AttributeHelpers::Collection::Hash>. It consumes
57L<MooseX::AttributeHelpers::MethodProvider::ImmutableHash>, and thus
58provides all its methods as wel.
5431dff2 59
60=head1 PROVIDED METHODS
61
62=over 4
63
64=item B<count>
65
720fa35b 66Returns the number of items in the hash.
5431dff2 67
720fa35b 68=item B<delete(@keys)>
5431dff2 69
720fa35b 70Deletes the specified keys from the hash.
8cf40f80 71
720fa35b 72=item B<clear>
5431dff2 73
720fa35b 74Deletes all keys from the hash.
5431dff2 75
76=item B<set>
77
720fa35b 78Sets the specified keys to the specified values. You can specify several of
79these at once, in key => value order.
9a976497 80
5431dff2 81=back
82
83=head1 BUGS
84
85All complex software has bugs lurking in it, and this module is no
86exception. If you find a bug please either email me, or add the bug
87to cpan-RT.
88
89=head1 AUTHOR
90
91Stevan Little E<lt>stevan@iinteractive.comE<gt>
92
93=head1 COPYRIGHT AND LICENSE
94
99c62fb8 95Copyright 2007-2008 by Infinity Interactive, Inc.
5431dff2 96
97L<http://www.iinteractive.com>
98
99This library is free software; you can redistribute it and/or modify
100it under the same terms as Perl itself.
101
102=cut
103