1 package Moose::AttributeHelpers::MethodProvider::Hash;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
8 with 'Moose::AttributeHelpers::MethodProvider::ImmutableHash';
11 my ($attr, $reader, $writer) = @_;
12 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
13 my $container_type_constraint = $attr->type_constraint->type_parameter;
15 my ( $self, @kvp ) = @_;
17 my ( @keys, @values );
20 my ( $key, $value ) = ( shift(@kvp), shift(@kvp) );
21 ($container_type_constraint->check($value))
22 || confess "Value " . ($value||'undef') . " did not pass container type constraint '$container_type_constraint'";
28 @{ $reader->($self) }{@keys} = @values;
30 $reader->($self)->{$keys[0]} = $values[0];
37 $reader->($_[0])->{$_[1]} = $_[2]
39 my ( $self, @kvp ) = @_;
40 my ( @keys, @values );
43 push @keys, shift @kvp;
44 push @values, shift @kvp;
47 @{ $reader->($_[0]) }{@keys} = @values;
53 sub accessor : method {
54 my ($attr, $reader, $writer) = @_;
56 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
57 my $container_type_constraint = $attr->type_constraint->type_parameter;
61 if (@_ == 1) { # reader
62 return $reader->($self)->{$_[0]};
64 elsif (@_ == 2) { # writer
65 ($container_type_constraint->check($_[1]))
66 || confess "Value " . ($_[1]||'undef') . " did not pass container type constraint '$container_type_constraint'";
67 $reader->($self)->{$_[0]} = $_[1];
70 confess "One or two arguments expected, not " . @_;
78 if (@_ == 1) { # reader
79 return $reader->($self)->{$_[0]};
81 elsif (@_ == 2) { # writer
82 $reader->($self)->{$_[0]} = $_[1];
85 confess "One or two arguments expected, not " . @_;
92 my ($attr, $reader, $writer) = @_;
93 return sub { %{$reader->($_[0])} = () };
97 my ($attr, $reader, $writer) = @_;
99 my $hashref = $reader->(shift);
100 CORE::delete @{$hashref}{@_};
112 Moose::AttributeHelpers::MethodProvider::Hash
116 This is a role which provides the method generators for
117 L<Moose::AttributeHelpers::Collection::Hash>.
119 This role is composed from the
120 L<Moose::AttributeHelpers::Collection::ImmutableHash> role.
130 =head1 PROVIDED METHODS
136 Returns the number of elements in the hash.
140 Removes the element with the given key
144 Returns true if the value of a given key is defined
148 If the list is populated, returns true. Otherwise, returns false.
152 Unsets the hash entirely.
156 Returns true if the given key is present in the hash
160 Returns an element of the hash by its key.
164 Returns the list of keys in the hash.
168 Sets the element in the hash at the given key to the given value.
172 Returns the list of values in the hash.
176 Returns the key, value pairs in the hash
180 If passed one argument, returns the value of the requested key. If passed two
181 arguments, sets the value of the requested key.
187 All complex software has bugs lurking in it, and this module is no
188 exception. If you find a bug please either email me, or add the bug
193 Stevan Little E<lt>stevan@iinteractive.comE<gt>
195 =head1 COPYRIGHT AND LICENSE
197 Copyright 2007-2009 by Infinity Interactive, Inc.
199 L<http://www.iinteractive.com>
201 This library is free software; you can redistribute it and/or modify
202 it under the same terms as Perl itself.