Commit | Line | Data |
65a43f48 |
1 | package MooseX::AttributeHelpers::MethodProvider::Hash; |
2 | use Moose::Role; |
720fa35b |
3 | use MooseX::AttributeHelpers::Collection::TypeCheck; |
65a43f48 |
4 | |
532b802c |
5 | our $VERSION = '0.04'; |
457dc4fb |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
9a976497 |
8 | with 'MooseX::AttributeHelpers::MethodProvider::ImmutableHash'; |
65a43f48 |
9 | |
10 | sub 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 |
30 | sub clear : method { |
31 | my ($attr, $reader, $writer) = @_; |
32 | return sub { %{$reader->($_[0])} = () }; |
33 | } |
34 | |
65a43f48 |
35 | sub 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 | |
43 | 1; |
44 | |
5431dff2 |
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 |
720fa35b |
56 | L<MooseX::AttributeHelpers::Collection::Hash>. It consumes |
57 | L<MooseX::AttributeHelpers::MethodProvider::ImmutableHash>, and thus |
58 | provides all its methods as wel. |
5431dff2 |
59 | |
60 | =head1 PROVIDED METHODS |
61 | |
62 | =over 4 |
63 | |
64 | =item B<count> |
65 | |
720fa35b |
66 | Returns the number of items in the hash. |
5431dff2 |
67 | |
720fa35b |
68 | =item B<delete(@keys)> |
5431dff2 |
69 | |
720fa35b |
70 | Deletes the specified keys from the hash. |
8cf40f80 |
71 | |
720fa35b |
72 | =item B<clear> |
5431dff2 |
73 | |
720fa35b |
74 | Deletes all keys from the hash. |
5431dff2 |
75 | |
76 | =item B<set> |
77 | |
720fa35b |
78 | Sets the specified keys to the specified values. You can specify several of |
79 | these at once, in key => value order. |
9a976497 |
80 | |
5431dff2 |
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 | |
99c62fb8 |
95 | Copyright 2007-2008 by Infinity Interactive, Inc. |
5431dff2 |
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 | |