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