Composite now implemented.
[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.03';
5 our $AUTHORITY = 'cpan:STEVAN';
6
7 sub exists : method {
8     my ($attr, $reader, $writer) = @_;    
9     return sub { CORE::exists $reader->($_[0])->{$_[1]} };
10 }   
11
12 sub get : method {
13     my ($attr, $reader, $writer) = @_;    
14     return sub { 
15         my ($self, @keys) = @_;
16         @{ $reader->($self) }{@keys} 
17     };
18 }
19
20 sub keys : method {
21     my ($attr, $reader, $writer) = @_;
22     return sub { CORE::keys %{$reader->($_[0])} };        
23 }
24      
25 sub values : method {
26     my ($attr, $reader, $writer) = @_;
27     return sub { CORE::values %{$reader->($_[0])} };        
28 }   
29
30 sub kv : method {
31     my ($attr, $reader, $writer) = @_;
32     return sub { 
33         my $h = $reader->($_[0]);
34         map {
35             [ $_, $h->{$_} ]
36         } CORE::keys %{$h} 
37     };    
38 }
39    
40 sub count : method {
41     my ($attr, $reader, $writer) = @_;
42     return sub { scalar CORE::keys %{$reader->($_[0])} };        
43 }
44
45 # Deprecated.  The author was thinking backwardsly when this was written.
46 sub empty : method {
47     my ($attr, $reader, $writer) = @_;
48     return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 };        
49 }
50
51 sub is_empty : method {
52     my ($attr, $reader, $writer) = @_;
53     return sub { CORE::keys %{$reader->($_[0])} == 0 };
54 }
55
56 sub has_items : method {
57     my ($attr, $reader, $writer) = @_;
58     return sub { CORE::keys %{$reader->($_[0])} > 0 };
59 }
60
61 1;
62
63 __END__
64
65 =pod
66
67 =head1 NAME
68
69 MooseX::AttributeHelpers::MethodProvider::ImmutableHash
70   
71 =head1 DESCRIPTION
72
73 This is a role which provides the method generators for 
74 L<MooseX::AttributeHelpers::Collection::ImmutableHash>.
75
76 =head1 PROVIDED METHODS
77
78 =over 4
79
80 =item B<count>
81
82 Returns the number of items in the hash.
83
84 =item B<empty>
85
86 DEPRECATED.  This was a misleading name for what it does (returns a boolean
87 indicating whether the hash is NOT empty), but we're keeping it for backwards
88 compatibility.  Do not use it in new code.  Use is_empty or has_items instead,
89 depending on what you meant.
90
91 =item B<is_empty>
92
93 Returns a boolean which is true if and only if the hash has no items in it.
94
95 =item B<has_items>
96
97 Returns a boolean which is true if and only if the hash has at least one item.
98
99 =item B<exists>
100
101 L<perlfunc/exists>
102
103 =item B<get(@keys)>
104
105 Gets the values specified by @keys from the hash.
106
107 =item B<keys>
108
109 L<perlfunc/keys>
110
111 =item B<values>
112
113 L<perlfunc/values>
114
115 =item B<kv>
116
117 Returns a list of arrayrefs, each of which is a key => value pair mapping.
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-2008 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