implement flatten
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
index ba2eb21..de8f2bb 100644 (file)
@@ -1,9 +1,7 @@
 package Moose::Autobox::Hash;
 use Moose::Role 'with';
 
-use Carp qw(croak);
-
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 with 'Moose::Autobox::Ref',
      'Moose::Autobox::Indexed';
@@ -15,11 +13,20 @@ sub delete {
 
 sub merge {
     my ($left, $right) = @_;
-    croak "You must pass a hashref as argument to merge"
+    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 {
@@ -52,6 +59,11 @@ 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" }
 
@@ -86,6 +98,12 @@ This is a role to describes a Hash value.
 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
@@ -104,6 +122,8 @@ shallow merging.
 
 =item B<kv>
 
+=item B<slice>
+
 =back
 
 =over 4
@@ -128,7 +148,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2007 by Infinity Interactive, Inc.
+Copyright 2006-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>