Bump to 0.19
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / ImmutableHash.pm
1 package MooseX::AttributeHelpers::MethodProvider::ImmutableHash;
2 use Moose::Role;
3
4 our $VERSION   = '0.19';
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 sub exists : method {
9     my ($attr, $reader, $writer) = @_;
10     return sub { CORE::exists $reader->($_[0])->{$_[1]} ? 1 : 0 };
11 }
12
13 sub defined : method {
14     my ($attr, $reader, $writer) = @_;
15     return sub { CORE::defined $reader->($_[0])->{$_[1]} ? 1 : 0 };
16 }
17
18 sub get : method {
19     my ($attr, $reader, $writer) = @_;
20     return sub {
21         if ( @_ == 2 ) {
22             $reader->($_[0])->{$_[1]}
23         } else {
24             my ( $self, @keys ) = @_;
25             @{ $reader->($self) }{@keys}
26         }
27     };
28 }
29
30 sub keys : method {
31     my ($attr, $reader, $writer) = @_;
32     return sub { CORE::keys %{$reader->($_[0])} };
33 }
34
35 sub values : method {
36     my ($attr, $reader, $writer) = @_;
37     return sub { CORE::values %{$reader->($_[0])} };
38 }
39
40 sub kv : method {
41     my ($attr, $reader, $writer) = @_;
42     return sub {
43         my $h = $reader->($_[0]);
44         map {
45             [ $_, $h->{$_} ]
46         } CORE::keys %{$h}
47     };
48 }
49
50 sub count : method {
51     my ($attr, $reader, $writer) = @_;
52     return sub { scalar CORE::keys %{$reader->($_[0])} };
53 }
54
55 sub empty : method {
56     my ($attr, $reader, $writer) = @_;
57     return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 };
58 }
59
60 1;
61
62 __END__
63
64 =pod
65
66 =head1 NAME
67
68 MooseX::AttributeHelpers::MethodProvider::ImmutableHash
69
70 =head1 DESCRIPTION
71
72 This is a role which provides the method generators for
73 L<MooseX::AttributeHelpers::Collection::ImmutableHash>.
74
75 =head1 METHODS
76
77 =over 4
78
79 =item B<meta>
80
81 =back
82
83 =head1 PROVIDED METHODS
84
85 =over 4
86
87 =item B<count>
88
89 Returns the number of elements in the list.
90
91 =item B<empty>
92
93 If the list is populated, returns true. Otherwise, returns false.
94
95 =item B<exists>
96
97 Returns true if the given key is present in the hash
98
99 =item B<defined>
100
101 Returns true if the value of a given key is defined
102
103 =item B<get>
104
105 Returns an element of the hash by its key.
106
107 =item B<keys>
108
109 Returns the list of keys in the hash.
110
111 =item B<values>
112
113 Returns the list of values in the hash.
114
115 =item B<kv>
116
117 Returns the  key, value pairs in the hash
118
119 =back
120
121 =head1 BUGS
122
123 All complex software has bugs lurking in it, and this module is no
124 exception. If you find a bug please either email me, or add the bug
125 to cpan-RT.
126
127 =head1 AUTHOR
128
129 Stevan Little E<lt>stevan@iinteractive.comE<gt>
130
131 =head1 COPYRIGHT AND LICENSE
132
133 Copyright 2007-2009 by Infinity Interactive, Inc.
134
135 L<http://www.iinteractive.com>
136
137 This library is free software; you can redistribute it and/or modify
138 it under the same terms as Perl itself.
139
140 =cut
141