ed2305216e554174846a6ad73d7f6122d99fdce3
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
1 package Moose::Autobox::Hash;
2 use Moose::Role 'with';
3
4 our $VERSION = '0.10';
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
85 # End Indexed
86
87 sub print   { CORE::print %{$_[0]} }
88 sub say     { CORE::print %{$_[0]}, "\n" }
89
90 1;
91
92 __END__
93
94 =pod
95
96 =head1 NAME 
97
98 Moose::Autobox::Hash - the Hash role
99
100 =head1 SYNOPOSIS
101
102   use Moose::Autobox;
103   
104   print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
105
106 =head1 DESCRIPTION
107
108 This is a role to describes a Hash value. 
109
110 =head1 METHODS
111
112 =over 4
113
114 =item B<delete>
115
116 =item B<merge>
117
118 Takes a hashref and returns a new hashref with right precedence
119 shallow merging.
120
121 =item B<hslice>
122
123 Slices a hash but returns the keys and values as a new hashref.
124
125 =item B<flatten>
126
127 =back
128
129 =head2 Indexed implementation
130
131 =over 4
132
133 =item B<at>
134
135 =item B<put>
136
137 =item B<exists>
138
139 =item B<keys>
140
141 =item B<values>
142
143 =item B<kv>
144
145 =item B<slice>
146
147 =item B<each>
148
149 =item B<each_key>
150
151 =item B<each_value>
152
153 =back
154
155 =over 4
156
157 =item B<meta>
158
159 =item B<print>
160
161 =item B<say>
162
163 =back
164
165 =head1 BUGS
166
167 All complex software has bugs lurking in it, and this module is no 
168 exception. If you find a bug please either email me, or add the bug
169 to cpan-RT.
170
171 =head1 AUTHOR
172
173 Stevan Little E<lt>stevan@iinteractive.comE<gt>
174
175 =head1 COPYRIGHT AND LICENSE
176
177 Copyright 2006-2008 by Infinity Interactive, Inc.
178
179 L<http://www.iinteractive.com>
180
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself.
183
184 =cut
185