CC: | Evan Carroll <me [...] evancarroll.com> |
Subject: | [PATCH] Evan Carroll -- ADD DEFINED AH FOR HASH |
Date: | Tue, 7 Apr 2009 12:21:23 -0500 |
To: | bug-MooseX-AttributeHelpers [...] rt.cpan.org |
From: | me+cpan [...] evancarroll.com |
From: Evan Carroll <me@evancarroll.com>
---
lib/MooseX/AttributeHelpers/Collection/Hash.pm | 2 +-
lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm | 2 ++
.../MethodProvider/ImmutableHash.pm | 5 +++++
t/003_basic_hash.t | 7 ++++++-
4 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/lib/MooseX/AttributeHelpers/Collection/Hash.pm b/lib/MooseX/AttributeHelpers/Collection/Hash.pm
index d8e64eb..a7ae349 100644
--- a/lib/MooseX/AttributeHelpers/Collection/Hash.pm
+++ b/lib/MooseX/AttributeHelpers/Collection/Hash.pm
@@ -56,7 +56,7 @@ MooseX::AttributeHelpers::Collection::Hash
=head1 DESCRIPTION
-This module provides an Hash attribute which provides a number of
+This module provides a Hash attribute which provides a number of
hash-like operations. See L<MooseX::AttributeHelpers::MethodProvider::Hash>
for more details.
diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm
index 0c98f0c..bb08d6c 100644
--- a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm
+++ b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm
@@ -97,6 +97,8 @@ L<MooseX::AttributeHelpers::Collection::ImmutableHash> role.
=item B<delete>
+=item B<defined>
+
=item B<empty>
=item B<clear>
diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm
index a118f41..36484e0 100644
--- a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm
+++ b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm
@@ -10,6 +10,11 @@ sub exists : method {
return sub { CORE::exists $reader->($_[0])->{$_[1]} ? 1 : 0 };
}
+sub defined : method {
+ my ($attr, $reader, $writer) = @_;
+ return sub { CORE::defined $reader->($_[0])->{$_[1]} ? 1 : 0 };
+}
+
sub get : method {
my ($attr, $reader, $writer) = @_;
return sub {
diff --git a/t/003_basic_hash.t b/t/003_basic_hash.t
index 7b1b779..a78d10b 100644
--- a/t/003_basic_hash.t
+++ b/t/003_basic_hash.t
@@ -3,7 +3,7 @@
use strict;
use warnings;
-use Test::More tests => 40;
+use Test::More tests => 42;
use Test::Exception;
BEGIN {
@@ -28,6 +28,7 @@ BEGIN {
'clear' => 'clear_options',
'delete' => 'delete_option',
'exists' => 'has_option',
+ 'defined'=> 'is_defined'
},
curries => {
'set' => {
@@ -47,6 +48,7 @@ can_ok($stuff, $_) for qw[
num_options
delete_option
clear_options
+ is_defined
has_option
];
@@ -60,6 +62,8 @@ lives_ok {
$stuff->set_option(foo => 'bar');
} '... set the option okay';
+ok($stuff->is_defined('foo'), '... foo is defined');
+
ok($stuff->has_options, '... we have options');
is($stuff->num_options, 1, '... we have 1 option(s)');
ok($stuff->has_option('foo'), '... we have a foo option');
@@ -134,6 +138,7 @@ is_deeply($options->provides, {
'count' => 'num_options',
'clear' => 'clear_options',
'delete' => 'delete_option',
+ 'defined'=> 'is_defined',
'exists' => 'has_option',
}, '... got the right provies mapping');
--
1.5.6.3