1 package Moose::Meta::Attribute::Trait::Native::MethodProvider::Hash;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
9 my ( $attr, $reader, $writer ) = @_;
10 return sub { CORE::exists $reader->( $_[0] )->{ $_[1] } ? 1 : 0 };
13 sub defined : method {
14 my ( $attr, $reader, $writer ) = @_;
15 return sub { CORE::defined $reader->( $_[0] )->{ $_[1] } ? 1 : 0 };
19 my ( $attr, $reader, $writer ) = @_;
22 $reader->( $_[0] )->{ $_[1] };
25 my ( $self, @keys ) = @_;
26 @{ $reader->($self) }{@keys};
32 my ( $attr, $reader, $writer ) = @_;
33 return sub { CORE::keys %{ $reader->( $_[0] ) } };
37 my ( $attr, $reader, $writer ) = @_;
38 return sub { CORE::values %{ $reader->( $_[0] ) } };
42 my ( $attr, $reader, $writer ) = @_;
44 my $h = $reader->( $_[0] );
45 map { [ $_, $h->{$_} ] } CORE::keys %{$h};
49 sub elements : method {
50 my ( $attr, $reader, $writer ) = @_;
52 my $h = $reader->( $_[0] );
53 map { $_, $h->{$_} } CORE::keys %{$h};
58 my ( $attr, $reader, $writer ) = @_;
59 return sub { scalar CORE::keys %{ $reader->( $_[0] ) } };
63 my ( $attr, $reader, $writer ) = @_;
64 return sub { scalar CORE::keys %{ $reader->( $_[0] ) } ? 1 : 0 };
69 my ( $attr, $reader, $writer ) = @_;
71 $attr->has_type_constraint
72 && $attr->type_constraint->isa(
73 'Moose::Meta::TypeConstraint::Parameterized')
75 my $container_type_constraint
76 = $attr->type_constraint->type_parameter;
78 my ( $self, @kvp ) = @_;
80 my ( @keys, @values );
83 my ( $key, $value ) = ( shift(@kvp), shift(@kvp) );
84 ( $container_type_constraint->check($value) )
86 . ( $value || 'undef' )
87 . " did not pass container type constraint '$container_type_constraint'";
93 @{ $reader->($self) }{@keys} = @values;
96 $reader->($self)->{ $keys[0] } = $values[0];
103 $reader->( $_[0] )->{ $_[1] } = $_[2];
106 my ( $self, @kvp ) = @_;
107 my ( @keys, @values );
110 push @keys, shift @kvp;
111 push @values, shift @kvp;
114 @{ $reader->( $_[0] ) }{@keys} = @values;
120 sub accessor : method {
121 my ( $attr, $reader, $writer ) = @_;
124 $attr->has_type_constraint
125 && $attr->type_constraint->isa(
126 'Moose::Meta::TypeConstraint::Parameterized')
128 my $container_type_constraint
129 = $attr->type_constraint->type_parameter;
133 if ( @_ == 1 ) { # reader
134 return $reader->($self)->{ $_[0] };
136 elsif ( @_ == 2 ) { # writer
137 ( $container_type_constraint->check( $_[1] ) )
139 . ( $_[1] || 'undef' )
140 . " did not pass container type constraint '$container_type_constraint'";
141 $reader->($self)->{ $_[0] } = $_[1];
144 confess "One or two arguments expected, not " . @_;
152 if ( @_ == 1 ) { # reader
153 return $reader->($self)->{ $_[0] };
155 elsif ( @_ == 2 ) { # writer
156 $reader->($self)->{ $_[0] } = $_[1];
159 confess "One or two arguments expected, not " . @_;
166 my ( $attr, $reader, $writer ) = @_;
167 return sub { %{ $reader->( $_[0] ) } = () };
170 sub delete : method {
171 my ( $attr, $reader, $writer ) = @_;
173 my $hashref = $reader->(shift);
174 CORE::delete @{$hashref}{@_};
186 Moose::Meta::Attribute::Trait::Native::MethodProvider::Hash
190 This is a role which provides the method generators for
191 L<Moose::Meta::Attribute::Trait::Native::Hash>.
193 This role is composed from the
194 L<Moose::Meta::Attribute::Trait::Native::ImmutableHash> role.
204 =head1 PROVIDED METHODS
210 Returns the number of elements in the list.
214 If the list is populated, returns true. Otherwise, returns false.
218 Returns true if the given key is present in the hash
222 Returns true if the value of a given key is defined
226 Returns an element of the hash by its key.
230 Returns the list of keys in the hash.
234 Returns the list of values in the hash.
238 Returns the key, value pairs in the hash as array references
242 Returns the key, value pairs in the hash as a flattened list
246 Removes the element with the given key
250 Unsets the hash entirely.
254 Sets the element in the hash at the given key to the given value.
258 If passed one argument, returns the value of the requested key. If passed two
259 arguments, sets the value of the requested key.
265 All complex software has bugs lurking in it, and this module is no
266 exception. If you find a bug please either email me, or add the bug
271 Stevan Little E<lt>stevan@iinteractive.comE<gt>
273 =head1 COPYRIGHT AND LICENSE
275 Copyright 2007-2009 by Infinity Interactive, Inc.
277 L<http://www.iinteractive.com>
279 This library is free software; you can redistribute it and/or modify
280 it under the same terms as Perl itself.