Docs tentatively finished.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Collection / ImmutableHash.pm
1 package MooseX::AttributeHelpers::MethodProvider::Collection::ImmutableHash;
2 use MooseX::AttributeHelpers::MethodProvider;
3
4 our $VERSION   = '0.03';
5 our $AUTHORITY = 'cpan:STEVAN';
6
7 add_method_provider 'Collection::ImmutableHash' => (
8     type => 'HashRef',
9     provides => {
10         exists => sub {
11             my ($attr, $reader, $writer) = @_;
12             return sub { CORE::exists $reader->($_[0])->{$_[1]} };
13         },
14
15         get => sub {
16             my ($attr, $reader, $writer) = @_;
17             return sub {
18                 my ($self, @keys) = @_;
19                 @{ $reader->($self) }{@keys}
20             };
21         },
22
23         keys => sub {
24             my ($attr, $reader, $writer) = @_;
25             return sub { CORE::keys %{$reader->($_[0])} };
26         },
27
28         values => sub {
29             my ($attr, $reader, $writer) = @_;
30             return sub { CORE::values %{$reader->($_[0])} };
31         },
32
33         kv => sub {
34             my ($attr, $reader, $writer) = @_;
35             return sub {
36                 my $h = $reader->($_[0]);
37                 map {
38                     [ $_, $h->{$_} ]
39                 } CORE::keys %{$h}
40             };
41         },
42
43         count => sub {
44             my ($attr, $reader, $writer) = @_;
45             return sub { scalar CORE::keys %{$reader->($_[0])} };
46         },
47
48         # Deprecated.  Does the opposite of what it claims to.
49         empty => sub {
50             my ($attr, $reader, $writer) = @_;
51             return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 };
52         },
53
54         is_empty => sub {
55             my ($attr, $reader, $writer) = @_;
56             return sub { CORE::keys %{$reader->($_[0])} == 0 };
57         },
58
59         has_items => sub {
60             my ($attr, $reader, $writer) = @_;
61             return sub { CORE::keys %{$reader->($_[0])} > 0 };
62         },
63     },
64 );
65
66 1;
67
68 __END__
69
70 =pod
71
72 =head1 NAME
73
74 MooseX::AttributeHelpers::MethodProvider::ImmutableHash
75
76 =head1 DESCRIPTION
77
78 This module provides the method factories for
79 L<MooseX::AttributeHelpers::Collection::ImmutableHash>.
80
81 =head1 PROVIDED METHODS
82
83 =over 4
84
85 =item B<count>
86
87 Returns the number of items in the hash.
88
89 =item B<empty>
90
91 DEPRECATED.  This was a misleading name for what it does (returns a boolean
92 indicating whether the hash is NOT empty), but we're keeping it for backwards
93 compatibility.  Do not use it in new code.  Use is_empty or has_items instead,
94 depending on what you meant.
95
96 =item B<is_empty>
97
98 Returns a boolean which is true if and only if the hash has no items in it.
99
100 =item B<has_items>
101
102 Returns a boolean which is true if and only if the hash has at least one item.
103
104 =item B<exists>
105
106 L<perlfunc/exists>
107
108 =item B<get(@keys)>
109
110 Gets the values specified by @keys from the hash.
111
112 =item B<keys>
113
114 L<perlfunc/keys>
115
116 =item B<values>
117
118 L<perlfunc/values>
119
120 =item B<kv>
121
122 Returns a list of arrayrefs, each of which is a key => value pair mapping.
123
124 =back
125
126 =head1 BUGS
127
128 All complex software has bugs lurking in it, and this module is no
129 exception. If you find a bug please either email me, or add the bug
130 to cpan-RT.
131
132 =head1 AUTHOR
133
134 Stevan Little E<lt>stevan@iinteractive.comE<gt>
135
136 =head1 COPYRIGHT AND LICENSE
137
138 Copyright 2007-2008 by Infinity Interactive, Inc.
139
140 L<http://www.iinteractive.com>
141
142 This library is free software; you can redistribute it and/or modify
143 it under the same terms as Perl itself.
144
145 =cut
146