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