docs
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
index 2f58a33..ed23052 100644 (file)
@@ -1,9 +1,7 @@
 package Moose::Autobox::Hash;
 use Moose::Role 'with';
 
-use Carp qw(croak);
-
-our $VERSION = '0.03';
+our $VERSION = '0.10';
 
 with 'Moose::Autobox::Ref',
      'Moose::Autobox::Indexed';
@@ -15,7 +13,7 @@ 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 };
 }
@@ -25,6 +23,10 @@ sub hslice {
     return { map { $_ => $hash->{$_} } @$keys };
 }
 
+sub flatten {
+    return %{$_[0]}
+}
+
 # ::Indexed implementation
 
 sub at {
@@ -60,7 +62,27 @@ sub kv {
 sub slice {
     my ($hash, $keys) = @_;
     return [ @{$hash}{@$keys} ];
-};
+}
+
+sub each {
+    my ($hash, $sub) = @_;
+    for my $key (CORE::keys %$hash) {
+      $sub->($key, $hash->{$key});
+    }
+}
+
+sub each_key {
+    my ($hash, $sub) = @_;
+    $sub->($_) for CORE::keys %$hash;
+}
+
+sub each_value {
+    my ($hash, $sub) = @_;
+    $sub->($_) for CORE::values %$hash;
+}
+
+
+# End Indexed
 
 sub print   { CORE::print %{$_[0]} }
 sub say     { CORE::print %{$_[0]}, "\n" }
@@ -100,6 +122,8 @@ shallow merging.
 
 Slices a hash but returns the keys and values as a new hashref.
 
+=item B<flatten>
+
 =back
 
 =head2 Indexed implementation
@@ -120,6 +144,12 @@ Slices a hash but returns the keys and values as a new hashref.
 
 =item B<slice>
 
+=item B<each>
+
+=item B<each_key>
+
+=item B<each_value>
+
 =back
 
 =over 4