Permalink
Browse files

just hacked up

  • Loading branch information...
1 parent add0bef commit 25350455504cc3cb1d4822c9cbaf33bdac3a42bc @kentaro committed Jun 23, 2011
Showing with 191 additions and 11 deletions.
  1. +4 −9 Makefile.PL
  2. +66 −2 lib/Hash/Compact.pm
  3. +121 −0 t/basic.t
  4. 0 t/{00_compile.t → compile.t}
View
@@ -2,20 +2,15 @@ use inc::Module::Install;
use Module::Install::AuthorTests;
use Module::Install::ReadmeMarkdownFromPod;
-name 'Hash-Compact';
+name 'Hash-Compact';
all_from 'lib/Hash/Compact.pm';
-readme_markdown_from 'lib/Hash/Compact.pm';
-
-# requires '';
-# requires 'Exporter' => 5.63; # use Exporter 'import';
-# requires 'Plack' => 0.9949;
+license 'MIT';
+readme_markdown_from 'lib/Hash/Compact.pm';
+requires 'Scalar::Util';
tests 't/*.t t/*/*.t t/*/*/*.t t/*/*/*/*.t';
test_requires 'Test::More' => 0.96; # done_testing, subtest
-# test_requires 'Test::Requires';
author_tests('xt');
-# test_requires 'YAML'; # required for use_test_base.
-# use_test_base;
WriteAll;
View
@@ -1,10 +1,74 @@
package Hash::Compact;
+use 5.008001;
use strict;
use warnings;
-use 5.008001;
-our $VERSION = '0.01';
+use Carp qw(croak);
+use Scalar::Util qw(blessed);
+our $VERSION = '0.01';
+sub new {
+ my $class = shift;
+ my $options = @_ > 1 && (ref $_[-1] || '') eq 'HASH' ? pop : {};
+ my $self = bless { __OPTIONS__ => $options }, $class;
+ my $args = shift || {};
+
+ croak '$args must be a hash-ref'
+ if (ref $args || '') ne 'HASH';
+
+ while (my ($key, $value) = each %$args) {
+ $self->param($key, $value);
+ }
+
+ $self;
+}
+
+sub options { $_[0]->{__OPTIONS__} }
+
+sub param {
+ my $self = shift;
+ my $value;
+
+ if (@_ > 1) {
+ croak 'incorrect key/value pair'
+ if @_ % 2;
+
+ my %args = @_;
+ while (my ($key, $value) = each %args) {
+ my $option = $self->options->{$key} || {};
+ $key = $option->{alias_for} || $key;
+
+ if (!ref $value && $value eq ($option->{default} || '')) {
+ delete $self->{$key};
+ }
+ else {
+ $self->{$key} = $value;
+ }
+ }
+ }
+ else {
+ my $key = shift;
+ my $option = $self->options->{$key} || {};
+ $value = $self->{$option->{alias_for} || $key} || $option->{default};
+ }
+
+ $value;
+}
+
+sub to_hash {
+ my $self = shift;
+ +{
+ map {
+ my $value = $self->{$_};
+ if (blessed $value && $value->can('to_hash')) {
+ $_ => $value->to_hash;
+ }
+ else {
+ $_ => $value;
+ }
+ }
+ grep { $_ ne '__OPTIONS__' } keys %$self }
+}
1;
__END__
View
121 t/basic.t
@@ -0,0 +1,121 @@
+use strict;
+use warnings;
+use Test::More;
+use Hash::Compact;
+
+subtest 'empty hash' => sub {
+ my $hash = Hash::Compact->new;
+
+ ok $hash;
+ isa_ok $hash, 'Hash::Compact';
+ is_deeply $hash->to_hash, +{};
+};
+
+subtest 'normal hash' => sub {
+ my $hash = Hash::Compact->new({
+ foo => 'foo',
+ bar => 'bar',
+ });
+
+ ok $hash;
+ isa_ok $hash, 'Hash::Compact';
+
+ is $hash->param('foo'), 'foo';
+ is $hash->param('bar'), 'bar';
+
+ $hash->param(baz => 'baz');
+ is $hash->param('baz'), 'baz';
+
+ is_deeply $hash->to_hash, +{
+ foo => 'foo',
+ bar => 'bar',
+ baz => 'baz',
+ };
+
+ done_testing;
+};
+
+subtest 'hash with options' => sub {
+ my $hash = Hash::Compact->new({
+ foo => 'foo',
+ },
+ {
+ foo => {
+ alias_for => 'f',
+ },
+ bar => {
+ default => 'bar',
+ },
+ baz => {
+ alias_for => 'b',
+ default => 'baz',
+ }
+ },
+ );
+
+ is $hash->param('foo'), 'foo';
+ is $hash->param('bar'), 'bar';
+ is $hash->param('baz'), 'baz';
+
+ is_deeply $hash->to_hash, +{
+ f => 'foo',
+ };
+
+ $hash->param(bar => 'hoge');
+ is $hash->param('bar'), 'hoge';
+ is_deeply $hash->to_hash, +{
+ f => 'foo',
+ bar => 'hoge',
+ };
+
+ $hash->param(bar => 'bar');
+ is $hash->param('bar'), 'bar';
+ ok !exists $hash->{bar};
+ is_deeply $hash->to_hash, +{
+ f => 'foo',
+ };
+
+ $hash->param(baz => 'fuga');
+ is $hash->param('baz'), 'fuga';
+ is_deeply $hash->to_hash, +{
+ f => 'foo',
+ b => 'fuga',
+ };
+
+ $hash->param(baz => 'baz');
+ is $hash->param('baz'), 'baz';
+ ok !exists $hash->{baz};
+ is_deeply $hash->to_hash, +{
+ f => 'foo',
+ };
+
+ done_testing;
+};
+
+subtest 'pass some refs' => sub {
+ my $hash = Hash::Compact->new;
+ $hash->param(array => [qw(foo bar)]);
+
+ is_deeply $hash->param('array'), +[qw(foo bar)];
+ is_deeply $hash->to_hash, +{
+ array => [qw(foo bar)]
+ };
+
+ my $hash2 = Hash::Compact->new({
+ baz => 'baz',
+ }, {
+ baz => {
+ alias_for => 'b',
+ },
+ });
+ $hash->param(hash => $hash2);
+
+ is_deeply $hash->to_hash, +{
+ array => [qw(foo bar)],
+ hash => {
+ b => 'baz',
+ },
+ };
+};
+
+done_testing;
File renamed without changes.

0 comments on commit 2535045

Please sign in to comment.