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 ) = @_;
13 $attr->has_type_constraint
14 && $attr->type_constraint->isa(
15 'Moose::Meta::TypeConstraint::Parameterized')
17 my $container_type_constraint
18 = $attr->type_constraint->type_parameter;
20 my ( $self, @kvp ) = @_;
22 my ( @keys, @values );
25 my ( $key, $value ) = ( shift(@kvp), shift(@kvp) );
26 ( $container_type_constraint->check($value) )
28 . ( $value || 'undef' )
29 . " did not pass container type constraint '$container_type_constraint'";
35 @{ $reader->($self) }{@keys} = @values;
38 $reader->($self)->{ $keys[0] } = $values[0];
45 $reader->( $_[0] )->{ $_[1] } = $_[2];
48 my ( $self, @kvp ) = @_;
49 my ( @keys, @values );
52 push @keys, shift @kvp;
53 push @values, shift @kvp;
56 @{ $reader->( $_[0] ) }{@keys} = @values;
62 sub accessor : method {
63 my ( $attr, $reader, $writer ) = @_;
66 $attr->has_type_constraint
67 && $attr->type_constraint->isa(
68 'Moose::Meta::TypeConstraint::Parameterized')
70 my $container_type_constraint
71 = $attr->type_constraint->type_parameter;
75 if ( @_ == 1 ) { # reader
76 return $reader->($self)->{ $_[0] };
78 elsif ( @_ == 2 ) { # writer
79 ( $container_type_constraint->check( $_[1] ) )
81 . ( $_[1] || 'undef' )
82 . " did not pass container type constraint '$container_type_constraint'";
83 $reader->($self)->{ $_[0] } = $_[1];
86 confess "One or two arguments expected, not " . @_;
94 if ( @_ == 1 ) { # reader
95 return $reader->($self)->{ $_[0] };
97 elsif ( @_ == 2 ) { # writer
98 $reader->($self)->{ $_[0] } = $_[1];
101 confess "One or two arguments expected, not " . @_;
108 my ( $attr, $reader, $writer ) = @_;
109 return sub { %{ $reader->( $_[0] ) } = () };
112 sub delete : method {
113 my ( $attr, $reader, $writer ) = @_;
115 my $hashref = $reader->(shift);
116 CORE::delete @{$hashref}{@_};
128 Moose::AttributeHelpers::MethodProvider::Hash
132 This is a role which provides the method generators for
133 L<Moose::AttributeHelpers::Collection::Hash>.
135 This role is composed from the
136 L<Moose::AttributeHelpers::Collection::ImmutableHash> role.
146 =head1 PROVIDED METHODS
152 Returns the number of elements in the hash.
156 Removes the element with the given key
160 Returns true if the value of a given key is defined
164 If the list is populated, returns true. Otherwise, returns false.
168 Unsets the hash entirely.
172 Returns true if the given key is present in the hash
176 Returns an element of the hash by its key.
180 Returns the list of keys in the hash.
184 Sets the element in the hash at the given key to the given value.
188 Returns the list of values in the hash.
192 Returns the key, value pairs in the hash
196 If passed one argument, returns the value of the requested key. If passed two
197 arguments, sets the value of the requested key.
203 All complex software has bugs lurking in it, and this module is no
204 exception. If you find a bug please either email me, or add the bug
209 Stevan Little E<lt>stevan@iinteractive.comE<gt>
211 =head1 COPYRIGHT AND LICENSE
213 Copyright 2007-2009 by Infinity Interactive, Inc.
215 L<http://www.iinteractive.com>
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.