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