8000d29f0c1cc1de54b285e6652b660ed90d5aab
[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.02';
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 print   { CORE::print %{$_[0]} }
56 sub say     { CORE::print %{$_[0]}, "\n" }
57
58 1;
59
60 __END__
61
62 =pod
63
64 =head1 NAME 
65
66 Moose::Autobox::Hash - the Hash role
67
68 =head1 SYNOPOSIS
69
70   use Moose::Autobox;
71   
72   print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
73
74 =head1 DESCRIPTION
75
76 This is a role to describes a Hash value. 
77
78 =head1 METHODS
79
80 =over 4
81
82 =item B<delete>
83
84 =item B<merge>
85
86 Takes a hashref and returns a new hashref with right precedence
87 shallow merging.
88
89 =back
90
91 =head2 Indexed implementation
92
93 =over 4
94
95 =item B<at>
96
97 =item B<put>
98
99 =item B<exists>
100
101 =item B<keys>
102
103 =item B<values>
104
105 =item B<kv>
106
107 =back
108
109 =over 4
110
111 =item B<meta>
112
113 =item B<print>
114
115 =item B<say>
116
117 =back
118
119 =head1 BUGS
120
121 All complex software has bugs lurking in it, and this module is no 
122 exception. If you find a bug please either email me, or add the bug
123 to cpan-RT.
124
125 =head1 AUTHOR
126
127 Stevan Little E<lt>stevan@iinteractive.comE<gt>
128
129 =head1 COPYRIGHT AND LICENSE
130
131 Copyright 2006-2008 by Infinity Interactive, Inc.
132
133 L<http://www.iinteractive.com>
134
135 This library is free software; you can redistribute it and/or modify
136 it under the same terms as Perl itself.
137
138 =cut
139