implement flatten
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
index 7fd63cd..de8f2bb 100644 (file)
@@ -1,7 +1,7 @@
 package Moose::Autobox::Hash;
 use Moose::Role 'with';
 
-our $VERSION = '0.01';
+our $VERSION = '0.03';
 
 with 'Moose::Autobox::Ref',
      'Moose::Autobox::Indexed';
@@ -11,6 +11,22 @@ sub delete {
     CORE::delete $hash->{$key}; 
 }
 
+sub merge {
+    my ($left, $right) = @_;
+    Carp::confess "You must pass a hashref as argument to merge"
+        unless ref $right eq 'HASH';
+    return { %$left, %$right };
+}
+
+sub hslice {
+    my ($hash, $keys) = @_;
+    return { map { $_ => $hash->{$_} } @$keys };
+}
+
+sub flatten {
+    return %{$_[0]}
+}
+
 # ::Indexed implementation
 
 sub at {
@@ -43,6 +59,14 @@ sub kv {
     [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ];    
 }
 
+sub slice {
+    my ($hash, $keys) = @_;
+    return [ @{$hash}{@$keys} ];
+};
+
+sub print   { CORE::print %{$_[0]} }
+sub say     { CORE::print %{$_[0]}, "\n" }
+
 1;
 
 __END__
@@ -69,6 +93,17 @@ This is a role to describes a Hash value.
 
 =item B<delete>
 
+=item B<merge>
+
+Takes a hashref and returns a new hashref with right precedence
+shallow merging.
+
+=item B<hslice>
+
+Slices a hash but returns the keys and values as a new hashref.
+
+=item B<flatten>
+
 =back
 
 =head2 Indexed implementation
@@ -87,12 +122,18 @@ This is a role to describes a Hash value.
 
 =item B<kv>
 
+=item B<slice>
+
 =back
 
 =over 4
 
 =item B<meta>
 
+=item B<print>
+
+=item B<say>
+
 =back
 
 =head1 BUGS
@@ -107,11 +148,12 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Infinity Interactive, Inc.
+Copyright 2006-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
-=cut
\ No newline at end of file
+=cut
+