Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CPANized

  • Loading branch information...
commit f1c2c49af8bf384b5d96546e83fe3a2f1fdf8724 1 parent dd2259f
@kentaro authored
Showing with 259 additions and 11 deletions.
  1. +12 −4 Makefile.PL
  2. +119 −2 README.mkdn
  3. +5 −3 eg/with_memcached.pl
  4. +123 −2 lib/Hash/Compact.pm
View
16 Makefile.PL
@@ -1,16 +1,24 @@
+sub author_tests {}
+sub readme_markdown_from {}
+sub auto_set_repository {}
+
use inc::Module::Install;
-use Module::Install::AuthorTests;
-use Module::Install::ReadmeMarkdownFromPod;
name 'Hash-Compact';
-all_from 'lib/Hash/Compact.pm';
-license 'MIT';
+author 'Kentaro Kuribayashi, <kentarok@cpan.org>';
+license 'Perl';
+all_from 'lib/Hash/Compact.pm';
readme_markdown_from 'lib/Hash/Compact.pm';
+repository 'https://github.com/kentaro/perl-hash-compact';
+bugtracker 'https://github.com/kentaro/perl-hash-compact/issues';
+auto_set_repository;
+
requires 'Scalar::Util';
tests 't/*.t t/*/*.t t/*/*/*.t t/*/*/*/*.t';
test_requires 'Test::More' => 0.96; # done_testing, subtest
author_tests('xt');
+
WriteAll;
View
121 README.mkdn
@@ -1,14 +1,131 @@
# NAME
-Hash::Compact -
+Hash::Compact - A hash-based object implementation with key alias and
+default value support
# SYNOPSIS
+ package My::Memcached;
+
+ use strict;
+ use warnings;
+ use parent qw(Cache::Memcached::Fast);
+
+ use JSON;
use Hash::Compact;
+ my $OPTIONS = {
+ foo => {
+ alias_for => 'f',
+ },
+ bar => {
+ alias_for => 'b',
+ default => 'bar',
+ },
+ };
+
+ sub get {
+ my ($self, $key) = @_;
+ my $value = $self->SUPER::get($key);
+ Hash::Compact->new(decode_json $value, $OPTIONS);
+ }
+
+ sub set {
+ my ($self, $key, $value, $expire) = @_;
+ my $hash = Hash::Compact->new($value, $OPTIONS);
+ $self->SUPER::set($key, encode_json $hash->to_hash, $expire);
+ }
+
+ package main;
+
+ use strict;
+ use warnings;
+ use Test::More;
+
+ my $key = 'key';
+ my $value = { foo => 'foo' };
+ my $memd = My::Memcached->new({servers => [qw(localhost:11211)]});
+ $memd->set($key, $value);
+
+ my $cached_value = $memd->get($key);
+ is_deeply $cached_value->param('foo'), 'foo';
+ is_deeply $cached_value->param('bar'), 'bar';
+ is_deeply $cached_value->to_hash, +{ f => 'foo' };
+
+ $cached_value->param(bar => 'baz');
+ $memd->set($key, $cached_value->to_hash);
+
+ $cached_value = $memd->get($key);
+ is_deeply $cached_value->param('foo'), 'foo';
+ is_deeply $cached_value->param('bar'), 'baz';
+ is_deeply $cached_value->to_hash, +{ f => 'foo', b => 'baz' };
+
+ done_testing;
+
# DESCRIPTION
-Hash::Compact is
+When we store some structured value into a column of a relational
+database or some key/value storage, redundancy of long key names can
+be a problem for storage space.
+
+This module is yet another hash-based object implementation which aims
+to be aware of both space efficiency and easiness to use for us.
+
+# METHODS
+
+## new (I<\%hash> I<[, \%options]>)
+
+ my $hash = Hash::Compact->new({
+ foo => 'foo',
+ }, {
+ foo => {
+ alias_for => 'f',
+ },
+ bar => {
+ alias_for => 'b',
+ default => 'bar',
+ },
+ },
+ );
+
+Creates and returns a new Hash::Compact object.
+
+`\%options` is a hash-ref which key/value pairs are associated with
+ones of `\%hash`. If it's not passed, Hash::Compact object `$hash`
+will be just a plain hash-based object.
+
+`\%options` may contain the fields below:
+
+- * alias_for
+
+Alias to an actual key. If it's passed, `\%hash` will be compacted
+into another hash which has aliased key. The original key of `\%hash`
+will be just an alias to an actual key.
+
+- * default
+
+If this exists and the value associated with the key of `\%hash` is
+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]>)
+
+ $hash->param('foo'); #=> 'foo'
+ $hash->param('bar'); #=> 'bar' (returns the default value)
+
+ $hash->param(bar => 'baz');
+ $hash->param('bar'); #=> 'baz'
+
+Setter/getter method.
+
+## to_hash ()
+
+ my $compact_hash_ref = $hash->to_hash;
+ #=> { f => 'foo', b => 'baz' } (returns a compacted hash)
+
+Returns a compacted hash according to `\%options` passed into the
+constructor above;
# AUTHOR
View
8 eg/with_memcached.pl
@@ -1,9 +1,11 @@
#!/usr/bin/env perl
+
use strict;
use warnings;
use Test::More;
package My::Memcached;
+
use strict;
use warnings;
use parent qw(Cache::Memcached::Fast);
@@ -11,7 +13,7 @@ package My::Memcached;
use JSON;
use Hash::Compact;
-my $options = {
+my $OPTIONS = {
foo => {
alias_for => 'f',
},
@@ -24,12 +26,12 @@ package My::Memcached;
sub get {
my ($self, $key) = @_;
my $value = $self->SUPER::get($key);
- Hash::Compact->new(decode_json $value, $options);
+ Hash::Compact->new(decode_json $value, $OPTIONS);
}
sub set {
my ($self, $key, $value, $expire) = @_;
- my $hash = Hash::Compact->new($value, $options);
+ my $hash = Hash::Compact->new($value, $OPTIONS);
$self->SUPER::set($key, encode_json $hash->to_hash, $expire);
}
View
125 lib/Hash/Compact.pm
@@ -71,21 +71,142 @@ sub to_hash {
}
1;
+
__END__
=encoding utf8
=head1 NAME
-Hash::Compact -
+Hash::Compact - A hash-based object implementation with key alias and
+default value support
=head1 SYNOPSIS
+ package My::Memcached;
+
+ use strict;
+ use warnings;
+ use parent qw(Cache::Memcached::Fast);
+
+ use JSON;
use Hash::Compact;
+ my $OPTIONS = {
+ foo => {
+ alias_for => 'f',
+ },
+ bar => {
+ alias_for => 'b',
+ default => 'bar',
+ },
+ };
+
+ sub get {
+ my ($self, $key) = @_;
+ my $value = $self->SUPER::get($key);
+ Hash::Compact->new(decode_json $value, $OPTIONS);
+ }
+
+ sub set {
+ my ($self, $key, $value, $expire) = @_;
+ my $hash = Hash::Compact->new($value, $OPTIONS);
+ $self->SUPER::set($key, encode_json $hash->to_hash, $expire);
+ }
+
+ package main;
+
+ use strict;
+ use warnings;
+ use Test::More;
+
+ my $key = 'key';
+ my $value = { foo => 'foo' };
+ my $memd = My::Memcached->new({servers => [qw(localhost:11211)]});
+ $memd->set($key, $value);
+
+ my $cached_value = $memd->get($key);
+ is_deeply $cached_value->param('foo'), 'foo';
+ is_deeply $cached_value->param('bar'), 'bar';
+ is_deeply $cached_value->to_hash, +{ f => 'foo' };
+
+ $cached_value->param(bar => 'baz');
+ $memd->set($key, $cached_value->to_hash);
+
+ $cached_value = $memd->get($key);
+ is_deeply $cached_value->param('foo'), 'foo';
+ is_deeply $cached_value->param('bar'), 'baz';
+ is_deeply $cached_value->to_hash, +{ f => 'foo', b => 'baz' };
+
+ done_testing;
+
=head1 DESCRIPTION
-Hash::Compact is
+When we store some structured value into a column of a relational
+database or some key/value storage, redundancy of long key names can
+be a problem for storage space.
+
+This module is yet another hash-based object implementation which aims
+to be aware of both space efficiency and easiness to use for us.
+
+=head1 METHODS
+
+=head2 new (I<\%hash> I<[, \%options]>)
+
+ my $hash = Hash::Compact->new({
+ foo => 'foo',
+ }, {
+ foo => {
+ alias_for => 'f',
+ },
+ bar => {
+ alias_for => 'b',
+ default => 'bar',
+ },
+ },
+ );
+
+Creates and returns a new Hash::Compact object. If C<\%options> not
+passed, Hash::Compact object C<$hash> will be just a plain hash-based
+object.
+
+C<\%options> is a hash-ref which key/value pairs are associated with
+ones of C<\%hash>. It may contain the fields below:
+
+=over 4
+
+=item * alias_for
+
+Alias to an actual key. If it's passed, C<\%hash> will be compacted
+into another hash which has aliased key. The original key of C<\%hash>
+will be just an alias to an actual key.
+
+=item * default
+
+If this exists and the value associated with the key of C<\%hash> is
+undefined, Hash::Compact object C<$hash> returns just the value. It's
+for space efficiency; C<$hash> doesn't need to have key/value pair
+when the value isn't defined or it's same as default value.
+
+=back
+
+=head2 param (I<$key> I<[, $value]>)
+
+ $hash->param('foo'); #=> 'foo'
+ $hash->param('bar'); #=> 'bar' (returns the default value)
+
+ $hash->param(bar => 'baz');
+ $hash->param('bar'); #=> 'baz'
+
+Setter/getter method.
+
+=head2 to_hash ()
+
+ my $compact_hash_ref = $hash->to_hash;
+ #=> { f => 'foo', b => 'baz' } (returns a compacted hash)
+
+Returns a compacted hash according to C<\%options> passed into the
+constructor above;
=head1 AUTHOR
Please sign in to comment.
Something went wrong with that request. Please try again.