From: Stevan Little Date: Fri, 1 Jun 2007 16:24:14 +0000 (+0000) Subject: foo X-Git-Tag: 0.18_01~73 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b9dc8e2f66cb0c06a8d52aaa3b4734237b749b20;p=gitmo%2FMooseX-AttributeHelpers.git foo --- diff --git a/lib/MooseX/AttributeHelpers/Collection/Array.pm b/lib/MooseX/AttributeHelpers/Collection/Array.pm index fbc3b1a..4bf17ca 100644 --- a/lib/MooseX/AttributeHelpers/Collection/Array.pm +++ b/lib/MooseX/AttributeHelpers/Collection/Array.pm @@ -6,89 +6,16 @@ use Moose::Util::TypeConstraints; our $VERSION = '0.01'; our $AUTHORITY = 'cpan:STEVAN'; -extends 'MooseX::AttributeHelpers::Collection'; +use MooseX::AttributeHelpers::MethodProvider::Array; -sub helper_type { 'ArrayRef' } +extends 'MooseX::AttributeHelpers::Collection'; -has '+method_constructors' => ( - default => sub { - return +{ - 'push' => sub { - my $attr = shift; - if ($attr->has_container_type) { - my $container_type_constraint = $attr->container_type_constraint; - return sub { - my $instance = shift; - $container_type_constraint->check($_) - || confess "Value " . ($_||'undef') . " did not pass container type constraint" - foreach @_; - push @{$attr->get_value($instance)} => @_; - }; - } - else { - return sub { - my $instance = shift; - push @{$attr->get_value($instance)} => @_; - }; - } - }, - 'pop' => sub { - my $attr = shift; - return sub { pop @{$attr->get_value($_[0])} }; - }, - 'unshift' => sub { - my $attr = shift; - if ($attr->has_container_type) { - my $container_type_constraint = $attr->container_type_constraint; - return sub { - my $instance = shift; - $container_type_constraint->check($_) - || confess "Value " . ($_||'undef') . " did not pass container type constraint" - foreach @_; - unshift @{$attr->get_value($instance)} => @_; - }; - } - else { - return sub { - my $instance = shift; - unshift @{$attr->get_value($instance)} => @_; - }; - } - }, - 'shift' => sub { - my $attr = shift; - return sub { shift @{$attr->get_value($_[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] }; - } - }, - 'count' => sub { - my $attr = shift; - return sub { scalar @{$attr->get_value($_[0])} }; - }, - 'empty' => sub { - my $attr = shift; - return sub { scalar @{$attr->get_value($_[0])} ? 1 : 0 }; - } - } - } +has '+method_provider' => ( + default => 'MooseX::AttributeHelpers::MethodProvider::Array' ); +sub helper_type { 'ArrayRef' } + no Moose; # register the alias ... diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm new file mode 100644 index 0000000..d953c97 --- /dev/null +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm @@ -0,0 +1,113 @@ +package MooseX::AttributeHelpers::MethodProvider::Array; +use Moose::Role; + +sub push : method { + my ($attr) = @_; + if ($attr->has_container_type) { + my $container_type_constraint = $attr->container_type_constraint; + return sub { + my $instance = CORE::shift; + $container_type_constraint->check($_) + || confess "Value " . ($_||'undef') . " did not pass container type constraint" + foreach @_; + CORE::push @{$attr->get_value($instance)} => @_; + }; + } + else { + return sub { + my $instance = CORE::shift; + CORE::push @{$attr->get_value($instance)} => @_; + }; + } +} + +sub pop : method { + my ($attr) = @_; + return sub { + CORE::pop @{$attr->get_value($_[0])} + }; +} + +sub unshift : method { + my ($attr) = @_; + if ($attr->has_container_type) { + my $container_type_constraint = $attr->container_type_constraint; + return sub { + my $instance = CORE::shift; + $container_type_constraint->check($_) + || confess "Value " . ($_||'undef') . " did not pass container type constraint" + foreach @_; + CORE::unshift @{$attr->get_value($instance)} => @_; + }; + } + else { + return sub { + my $instance = CORE::shift; + CORE::unshift @{$attr->get_value($instance)} => @_; + }; + } +} + +sub shift : method { + my ($attr) = @_; + return sub { + CORE::shift @{$attr->get_value($_[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 count : method { + my ($attr) = @_; + return sub { + scalar @{$attr->get_value($_[0])} + }; +} + +sub empty : method { + my ($attr) = @_; + return sub { + scalar @{$attr->get_value($_[0])} ? 1 : 0 + }; +} + +sub find : method { + my ($attr) = @_; + return sub { + my ($instance, $predicate) = @_; + foreach my $val (@{$attr->get_value($instance)}) { + return $val if $predicate->($val); + } + return; + }; +} + +1; + +__END__ + +=pod + +=cut