1 package Moose::Meta::Attribute::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] ) } };
62 sub is_empty : method {
63 my ( $attr, $reader, $writer ) = @_;
64 return sub { scalar CORE::keys %{ $reader->( $_[0] ) } ? 0 : 1 };
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::Native::MethodProvider::Hash - role providing method generators for Hash trait
190 This is a role which provides the method generators for
191 L<Moose::Meta::Attribute::Native::Trait::Hash>. Please check there for
192 documentation on what methods are provided.
204 See L<Moose/BUGS> for details on reporting bugs.
208 Stevan Little E<lt>stevan@iinteractive.comE<gt>
210 =head1 COPYRIGHT AND LICENSE
212 Copyright 2007-2009 by Infinity Interactive, Inc.
214 L<http://www.iinteractive.com>
216 This library is free software; you can redistribute it and/or modify
217 it under the same terms as Perl itself.