From: Stevan Little Date: Fri, 20 Jul 2007 02:25:10 +0000 (+0000) Subject: foo X-Git-Tag: 0.18_01~68 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=65a43f48f3a802173ac29947be044bd317b0fd71;hp=158d8e2026314d8863d02a2a8bac6f8d66215e66;p=gitmo%2FMooseX-AttributeHelpers.git foo --- diff --git a/lib/MooseX/AttributeHelpers/Collection/Hash.pm b/lib/MooseX/AttributeHelpers/Collection/Hash.pm index 566972c..01c5e50 100644 --- a/lib/MooseX/AttributeHelpers/Collection/Hash.pm +++ b/lib/MooseX/AttributeHelpers/Collection/Hash.pm @@ -5,59 +5,16 @@ use Moose; our $VERSION = '0.01'; our $AUTHORITY = 'cpan:STEVAN'; -extends 'MooseX::AttributeHelpers::Collection'; +use MooseX::AttributeHelpers::MethodProvider::Hash; -sub helper_type { 'HashRef' } +extends 'MooseX::AttributeHelpers::Collection'; -has '+method_constructors' => ( - default => sub { - return +{ - 'exists' => sub { - my $attr = shift; - return sub { exists $attr->get_value($_[0])->{$_[1]} ? 1 : 0 }; - }, - 'get' => sub { - my $attr = shift; - return sub { $attr->get_value($_[0])->{$_[1]} }; - }, - 'set' => sub { - my $attr = shift; - if ($attr->has_container_type) { - my $container_type_constraint = $attr->container_type_constraint; - return sub { - ($container_type_constraint->check($_[2])) - || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; - $attr->get_value($_[0])->{$_[1]} = $_[2] - }; - } - else { - return sub { $attr->get_value($_[0])->{$_[1]} = $_[2] }; - } - }, - 'keys' => sub { - my $attr = shift; - return sub { keys %{$attr->get_value($_[0])} }; - }, - 'values' => sub { - my $attr = shift; - return sub { values %{$attr->get_value($_[0])} }; - }, - 'count' => sub { - my $attr = shift; - return sub { scalar keys %{$attr->get_value($_[0])} }; - }, - 'empty' => sub { - my $attr = shift; - return sub { scalar keys %{$attr->get_value($_[0])} ? 1 : 0 }; - }, - 'delete' => sub { - my $attr = shift; - return sub { delete $attr->get_value($_[0])->{$_[1]} }; - } - } - } +has '+method_provider' => ( + default => 'MooseX::AttributeHelpers::MethodProvider::Hash' ); +sub helper_type { 'HashRef' } + no Moose; # register the alias ... diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm new file mode 100644 index 0000000..bca2cd3 --- /dev/null +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm @@ -0,0 +1,56 @@ +package MooseX::AttributeHelpers::MethodProvider::Hash; +use Moose::Role; + +sub exists : method { + my ($attr) = @_; + return sub { exists $attr->get_value($_[0])->{$_[1]} ? 1 : 0 }; +} + +sub get : method { + my ($attr) = @_; + return sub { $attr->get_value($_[0])->{$_[1]} }; +} + +sub set : method { + my ($attr) = @_; + if ($attr->has_container_type) { + my $container_type_constraint = $attr->container_type_constraint; + return sub { + ($container_type_constraint->check($_[2])) + || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; + $attr->get_value($_[0])->{$_[1]} = $_[2] + }; + } + else { + return sub { $attr->get_value($_[0])->{$_[1]} = $_[2] }; + } +} + +sub keys : method { + my ($attr) = @_; + return sub { keys %{$attr->get_value($_[0])} }; +} + +sub values : method { + my ($attr) = @_; + return sub { values %{$attr->get_value($_[0])} }; +} + +sub count : method { + my ($attr) = @_; + return sub { scalar keys %{$attr->get_value($_[0])} }; +} + +sub empty : method { + my ($attr) = @_; + return sub { scalar keys %{$attr->get_value($_[0])} ? 1 : 0 }; +} + +sub delete : method { + my ($attr) = @_; + return sub { delete $attr->get_value($_[0])->{$_[1]} }; +} + +1; + +__END__ \ No newline at end of file