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