f1b5dc4ee62c0f6092292a7b9ab8ce34e01bc546
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
1 package Moose::Autobox::Hash;
2 use Moose::Role 'with';
3
4 our $VERSION = '0.11';
5
6 with 'Moose::Autobox::Ref',
7      'Moose::Autobox::Indexed';
8
9 sub delete { 
10     my ($hash, $key) = @_;
11     CORE::delete $hash->{$key}; 
12 }
13
14 sub merge {
15     my ($left, $right) = @_;
16     Carp::confess "You must pass a hashref as argument to merge"
17         unless ref $right eq 'HASH';
18     return { %$left, %$right };
19 }
20
21 sub hslice {
22     my ($hash, $keys) = @_;
23     return { map { $_ => $hash->{$_} } @$keys };
24 }
25
26 sub flatten {
27     return %{$_[0]}
28 }
29
30 # ::Indexed implementation
31
32 sub at {
33     my ($hash, $index) = @_;
34     $hash->{$index};
35
36
37 sub put {
38     my ($hash, $index, $value) = @_;
39     $hash->{$index} = $value;
40 }
41
42 sub exists { 
43     my ($hash, $key) = @_;
44     CORE::exists $hash->{$key}; 
45 }
46
47 sub keys { 
48     my ($hash) = @_;
49     [ CORE::keys %$hash ];
50 }
51
52 sub values { 
53     my ($hash) = @_;    
54     [ CORE::values %$hash ]; 
55 }
56
57 sub kv {
58     my ($hash) = @_;    
59     [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ];    
60 }
61
62 sub slice {
63     my ($hash, $keys) = @_;
64     return [ @{$hash}{@$keys} ];
65 }
66
67 sub each {
68     my ($hash, $sub) = @_;
69     for my $key (CORE::keys %$hash) {
70       $sub->($key, $hash->{$key});
71     }
72 }
73
74 sub each_key {
75     my ($hash, $sub) = @_;
76     $sub->($_) for CORE::keys %$hash;
77 }
78
79 sub each_value {
80     my ($hash, $sub) = @_;
81     $sub->($_) for CORE::values %$hash;
82 }
83
84 sub each_n_values {
85     my ($hash, $n, $sub) = @_;
86     my @keys = CORE::keys %$hash;
87     my $it = List::MoreUtils::natatime($n, @keys);
88
89     while (my @vals = $it->()) {
90         $sub->(@$hash{ @vals });
91     }
92
93     return;
94 }
95
96
97 # End Indexed
98
99 sub print   { CORE::print %{$_[0]} }
100 sub say     { CORE::print %{$_[0]}, "\n" }
101
102 1;
103
104 __END__
105
106 =pod
107
108 =head1 NAME 
109
110 Moose::Autobox::Hash - the Hash role
111
112 =head1 SYNOPOSIS
113
114   use Moose::Autobox;
115   
116   print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
117
118 =head1 DESCRIPTION
119
120 This is a role to describes a Hash value. 
121
122 =head1 METHODS
123
124 =over 4
125
126 =item B<delete>
127
128 =item B<merge>
129
130 Takes a hashref and returns a new hashref with right precedence
131 shallow merging.
132
133 =item B<hslice>
134
135 Slices a hash but returns the keys and values as a new hashref.
136
137 =item B<flatten>
138
139 =back
140
141 =head2 Indexed implementation
142
143 =over 4
144
145 =item B<at>
146
147 =item B<put>
148
149 =item B<exists>
150
151 =item B<keys>
152
153 =item B<values>
154
155 =item B<kv>
156
157 =item B<slice>
158
159 =item B<each>
160
161 =item B<each_key>
162
163 =item B<each_value>
164
165 =item B<each_n_values>
166
167 =back
168
169 =over 4
170
171 =item B<meta>
172
173 =item B<print>
174
175 =item B<say>
176
177 =back
178
179 =head1 BUGS
180
181 All complex software has bugs lurking in it, and this module is no 
182 exception. If you find a bug please either email me, or add the bug
183 to cpan-RT.
184
185 =head1 AUTHOR
186
187 Stevan Little E<lt>stevan@iinteractive.comE<gt>
188
189 =head1 COPYRIGHT AND LICENSE
190
191 Copyright 2006-2008 by Infinity Interactive, Inc.
192
193 L<http://www.iinteractive.com>
194
195 This library is free software; you can redistribute it and/or modify
196 it under the same terms as Perl itself.
197
198 =cut
199