Permalink
Browse files

- added: keys(), original()

- added: compact() as an alias to to_hash()
- to_hash() is now deprecated and will be removed at later version
  • Loading branch information...
1 parent 73e465a commit 381595f4b9f9c6d35db9c47ae435dd4256ab0508 @kentaro committed Nov 10, 2011
Showing with 213 additions and 36 deletions.
  1. +9 −0 Changes
  2. +35 −9 README.mkdn
  3. +70 −13 lib/Hash/Compact.pm
  4. +99 −14 t/basic.t
View
@@ -1,5 +1,14 @@
Revision history for Perl extension Hash::Compact
+0.05 2011-11-10
+
+ [FEATURE CHANGES]
+ - added: keys(), original()
+ - added: compact() as an alias to to_hash()
+
+ [IMPORTANT NOTICE]
+ - to_hash() is now deprecated and will be removed at later version
+
0.04 2011-11-04
[TRIBIAL CHANGES]
View
@@ -33,7 +33,7 @@ default value support
sub set {
my ($self, $key, $value, $expire) = @_;
my $hash = Hash::Compact->new($value, $OPTIONS);
- $self->SUPER::set($key, encode_json $hash->to_hash, $expire);
+ $self->SUPER::set($key, encode_json $hash->compact, $expire);
}
package main;
@@ -50,15 +50,15 @@ default value support
my $cached_value = $memd->get($key);
is $cached_value->param('foo'), 'foo';
is $cached_value->param('bar'), 'bar';
- is_deeply $cached_value->to_hash, +{ f => 'foo' };
+ is_deeply $cached_value->compact, +{ f => 'foo' };
$cached_value->param(bar => 'baz');
- $memd->set($key, $cached_value->to_hash);
+ $memd->set($key, $cached_value->compact);
$cached_value = $memd->get($key);
is $cached_value->param('foo'), 'foo';
is $cached_value->param('bar'), 'baz';
- is_deeply $cached_value->to_hash, +{ f => 'foo', b => 'baz' };
+ is_deeply $cached_value->compact, +{ f => 'foo', b => 'baz' };
done_testing;
@@ -108,24 +108,50 @@ undefined, Hash::Compact object `$hash` returns just the value. It's
for space efficiency; `$hash` doesn't need to have key/value pair
when the value isn't defined or it's same as default value.
-## param (I<$key> I<[, $value]>)
+## param (I<$key>)
+
+## param (I<%pairs>)
$hash->param('foo'); #=> 'foo'
$hash->param('bar'); #=> 'bar' (returns the default value)
- $hash->param(bar => 'baz');
+ $hash->param(
+ bar => 'baz',
+ qux => 'quux',
+ );
$hash->param('bar'); #=> 'baz'
Setter/getter method.
-## to_hash ()
+## compact ()
- my $compact_hash_ref = $hash->to_hash;
- #=> { f => 'foo', b => 'baz' } (returns a compacted hash)
+ my $compact_hash_ref = $hash->compact;
+ #=> { f => 'foo', b => 'baz' qux => 'quux' } (returns a compacted hash)
Returns a compacted hash according to `\%options` passed into the
constructor above;
+## to_hash ()
+
+This method will be deprecated and removed at later version.
+
+## keys ()
+
+ @keys = $hash->keys; #=> (foo, bar, qux)
+
+Returns the original key names. If `default` option is set for a key,
+the key will be returned even if the value associated with the key is
+not set.
+
+## original ()
+
+ my $original_hash_ref = $hash->original;
+ #=> { foo => 'foo', bar => 'baz' qux => 'quux' } (returns an original hash)
+
+Returns the original key-value pairs as HashRef, which includes
+key-value pairs if the key-values not set but `default` option is
+designated.
+
# AUTHOR
Kentaro Kuribayashi <kentarok@gmail.com>
View
@@ -25,6 +25,27 @@ sub new {
sub options { $_[0]->{__HASH_COMPACT_OPTIONS__} }
+sub keys {
+ my $self = shift;
+ my %alias_map;
+ my @defaults;
+
+ for my $key (CORE::keys %{$self->options}) {
+ if (my $raw = $self->options->{$key}{alias_for}) {
+ $alias_map{$raw} = $key;
+ }
+ if ($self->options->{$key}{default}) {
+ push @defaults, $key;
+ }
+ }
+
+ my %seen;
+ grep { !$seen{$_}++ } map {
+ my $key = $_;
+ my $original_key = $alias_map{$key} ? $alias_map{$key} : $key;
+ } grep { $_ ne '__HASH_COMPACT_OPTIONS__' } (keys %$self, @defaults);
+}
+
sub param {
my $self = shift;
my $value;
@@ -57,23 +78,33 @@ sub param {
}
sub to_hash {
+ warn 'to_hash() method will be deprecated at later version. use compact() instead';
+ $_[0]->compact;
+}
+
+sub compact {
my $self = shift;
+{
map {
my $value = $self->{$_};
+
if (blessed $value && $value->can('to_hash')) {
- $_ => $value->to_hash;
+ $_ => $value->compact;
}
else {
$_ => $value;
}
- }
- grep { $_ ne '__HASH_COMPACT_OPTIONS__' } keys %$self
+ } grep { $_ ne '__HASH_COMPACT_OPTIONS__' } CORE::keys %$self
}
}
-1;
+sub original {
+ my $self = shift;
+ +{ map { $_ => $self->param($_) } $self->keys }
+}
+
+!!1;
__END__
@@ -114,7 +145,7 @@ default value support
sub set {
my ($self, $key, $value, $expire) = @_;
my $hash = Hash::Compact->new($value, $OPTIONS);
- $self->SUPER::set($key, encode_json $hash->to_hash, $expire);
+ $self->SUPER::set($key, encode_json $hash->compact, $expire);
}
package main;
@@ -131,15 +162,15 @@ default value support
my $cached_value = $memd->get($key);
is $cached_value->param('foo'), 'foo';
is $cached_value->param('bar'), 'bar';
- is_deeply $cached_value->to_hash, +{ f => 'foo' };
+ is_deeply $cached_value->compact, +{ f => 'foo' };
$cached_value->param(bar => 'baz');
- $memd->set($key, $cached_value->to_hash);
+ $memd->set($key, $cached_value->compact);
$cached_value = $memd->get($key);
is $cached_value->param('foo'), 'foo';
is $cached_value->param('bar'), 'baz';
- is_deeply $cached_value->to_hash, +{ f => 'foo', b => 'baz' };
+ is_deeply $cached_value->compact, +{ f => 'foo', b => 'baz' };
done_testing;
@@ -193,24 +224,50 @@ when the value isn't defined or it's same as default value.
=back
-=head2 param (I<$key> I<[, $value]>)
+=head2 param (I<$key>)
+
+=head2 param (I<%pairs>)
$hash->param('foo'); #=> 'foo'
$hash->param('bar'); #=> 'bar' (returns the default value)
- $hash->param(bar => 'baz');
+ $hash->param(
+ bar => 'baz',
+ qux => 'quux',
+ );
$hash->param('bar'); #=> 'baz'
Setter/getter method.
-=head2 to_hash ()
+=head2 compact ()
- my $compact_hash_ref = $hash->to_hash;
- #=> { f => 'foo', b => 'baz' } (returns a compacted hash)
+ my $compact_hash_ref = $hash->compact;
+ #=> { f => 'foo', b => 'baz' qux => 'quux' } (returns a compacted hash)
Returns a compacted hash according to C<\%options> passed into the
constructor above;
+=head2 to_hash ()
+
+This method will be deprecated and removed at later version.
+
+=head2 keys ()
+
+ @keys = $hash->keys; #=> (foo, bar, qux)
+
+Returns the original key names. If C<default> option is set for a key,
+the key will be returned even if the value associated with the key is
+not set.
+
+=head2 original ()
+
+ my $original_hash_ref = $hash->original;
+ #=> { foo => 'foo', bar => 'baz' qux => 'quux' } (returns an original hash)
+
+Returns the original key-value pairs as HashRef, which includes
+key-value pairs if the key-values not set but C<default> option is
+designated.
+
=head1 AUTHOR
Kentaro Kuribayashi E<lt>kentarok@gmail.comE<gt>
Oops, something went wrong.

0 comments on commit 381595f

Please sign in to comment.