Docs tentatively finished.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Collection / ImmutableHash.pm
CommitLineData
1b45ecc4 1package MooseX::AttributeHelpers::MethodProvider::Collection::ImmutableHash;
2use MooseX::AttributeHelpers::MethodProvider;
3
4our $VERSION = '0.03';
5our $AUTHORITY = 'cpan:STEVAN';
6
7add_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
661;
67
68__END__
69
70=pod
71
72=head1 NAME
73
74MooseX::AttributeHelpers::MethodProvider::ImmutableHash
75
76=head1 DESCRIPTION
77
1d458eb8 78This module provides the method factories for
1b45ecc4 79L<MooseX::AttributeHelpers::Collection::ImmutableHash>.
80
81=head1 PROVIDED METHODS
82
83=over 4
84
85=item B<count>
86
87Returns the number of items in the hash.
88
89=item B<empty>
90
91DEPRECATED. This was a misleading name for what it does (returns a boolean
92indicating whether the hash is NOT empty), but we're keeping it for backwards
93compatibility. Do not use it in new code. Use is_empty or has_items instead,
94depending on what you meant.
95
96=item B<is_empty>
97
98Returns a boolean which is true if and only if the hash has no items in it.
99
100=item B<has_items>
101
102Returns a boolean which is true if and only if the hash has at least one item.
103
104=item B<exists>
105
106L<perlfunc/exists>
107
108=item B<get(@keys)>
109
110Gets the values specified by @keys from the hash.
111
112=item B<keys>
113
114L<perlfunc/keys>
115
116=item B<values>
117
118L<perlfunc/values>
119
120=item B<kv>
121
122Returns a list of arrayrefs, each of which is a key => value pair mapping.
123
124=back
125
126=head1 BUGS
127
128All complex software has bugs lurking in it, and this module is no
129exception. If you find a bug please either email me, or add the bug
130to cpan-RT.
131
132=head1 AUTHOR
133
134Stevan Little E<lt>stevan@iinteractive.comE<gt>
135
136=head1 COPYRIGHT AND LICENSE
137
138Copyright 2007-2008 by Infinity Interactive, Inc.
139
140L<http://www.iinteractive.com>
141
142This library is free software; you can redistribute it and/or modify
143it under the same terms as Perl itself.
144
145=cut
146