}
}
+sub accessor : method {
+ my ($attr, $reader, $writer) = @_;
+
+ if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
+ my $container_type_constraint = $attr->type_constraint->type_parameter;
+ return sub {
+ my $self = shift;
+
+ if (@_ == 1) { # reader
+ return $reader->($self)->{$_[0]};
+ }
+ elsif (@_ == 2) { # writer
+ ($container_type_constraint->check($_[1]))
+ || confess "Value " . ($_[1]||'undef') . " did not pass container type constraint";
+ $reader->($self)->{$_[0]} = $_[1];
+ }
+ else {
+ confess "One or two arguments expected, not " . @_;
+ }
+ };
+ }
+ else {
+ return sub {
+ my $self = shift;
+
+ if (@_ == 1) { # reader
+ return $reader->($self)->{$_[0]};
+ }
+ elsif (@_ == 2) { # writer
+ $reader->($self)->{$_[0]} = $_[1];
+ }
+ else {
+ confess "One or two arguments expected, not " . @_;
+ }
+ };
+ }
+}
+
sub clear : method {
my ($attr, $reader, $writer) = @_;
return sub { %{$reader->($_[0])} = () };
Returns the key, value pairs in the hash
+=item B<accessor>
+
+If passed one argument, returns the value of the requested key. If passed two
+arguments, sets the value of the requested key.
+
=back
=head1 BUGS
use strict;
use warnings;
-use Test::More tests => 42;
+use Test::More tests => 45;
use Test::Exception;
BEGIN {
isa => 'HashRef[Str]',
default => sub { {} },
provides => {
- 'set' => 'set_option',
- 'get' => 'get_option',
- 'empty' => 'has_options',
- 'count' => 'num_options',
- 'clear' => 'clear_options',
- 'delete' => 'delete_option',
- 'exists' => 'has_option',
- 'defined'=> 'is_defined',
+ 'set' => 'set_option',
+ 'get' => 'get_option',
+ 'empty' => 'has_options',
+ 'count' => 'num_options',
+ 'clear' => 'clear_options',
+ 'delete' => 'delete_option',
+ 'exists' => 'has_option',
+ 'defined' => 'is_defined',
+ 'accessor' => 'option_accessor',
},
curries => {
- 'set' => {
- set_quantity => ['quantity']
+ 'accessor' => {
+ quantity => ['quantity'],
},
}
);
clear_options
is_defined
has_option
+ quantity
+ option_accessor
];
ok(!$stuff->has_options, '... we have no options');
is_deeply($stuff->options, { }, "... cleared options" );
lives_ok {
- $stuff->set_quantity(4);
+ $stuff->quantity(4);
} '... options added okay with defaults';
+is($stuff->quantity, 4, 'reader part of curried accessor works');
+
is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
lives_ok {
isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
is_deeply($options->provides, {
- 'set' => 'set_option',
- 'get' => 'get_option',
- 'empty' => 'has_options',
- 'count' => 'num_options',
- 'clear' => 'clear_options',
- 'delete' => 'delete_option',
- 'defined' => 'is_defined',
- 'exists' => 'has_option',
-}, '... got the right provies mapping');
+ 'set' => 'set_option',
+ 'get' => 'get_option',
+ 'empty' => 'has_options',
+ 'count' => 'num_options',
+ 'clear' => 'clear_options',
+ 'delete' => 'delete_option',
+ 'defined' => 'is_defined',
+ 'exists' => 'has_option',
+ 'accessor' => 'option_accessor',
+}, '... got the right provides mapping');
is($options->type_constraint->type_parameter, 'Str', '... got the right container type');