Bump to 0.12
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Hash.pm
CommitLineData
65a43f48 1package MooseX::AttributeHelpers::MethodProvider::Hash;
2use Moose::Role;
3
3f58c364 4our $VERSION = '0.12';
457dc4fb 5our $AUTHORITY = 'cpan:STEVAN';
6
9a976497 7with 'MooseX::AttributeHelpers::MethodProvider::ImmutableHash';
65a43f48 8
9sub set : method {
457dc4fb 10 my ($attr, $reader, $writer) = @_;
9a976497 11 if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
12 my $container_type_constraint = $attr->type_constraint->type_parameter;
65a43f48 13 return sub {
05f7da43 14 my ( $self, @kvp ) = @_;
15
16 my ( @keys, @values );
17
18 while ( @kvp ) {
19 my ( $key, $value ) = ( shift(@kvp), shift(@kvp) );
20 ($container_type_constraint->check($value))
21 || confess "Value " . ($value||'undef') . " did not pass container type constraint";
22 push @keys, $key;
23 push @values, $value;
24 }
25
26 if ( @values > 1 ) {
27 @{ $reader->($self) }{@keys} = @values;
28 } else {
29 $reader->($self)->{$keys[0]} = $values[0];
30 }
65a43f48 31 };
32 }
33 else {
05f7da43 34 return sub {
35 if ( @_ == 3 ) {
36 $reader->($_[0])->{$_[1]} = $_[2]
37 } else {
38 my ( $self, @kvp ) = @_;
39 my ( @keys, @values );
40
41 while ( @kvp ) {
42 push @keys, shift @kvp;
43 push @values, shift @kvp;
44 }
45
37cd0795 46 @{ $reader->($_[0]) }{@keys} = @values;
05f7da43 47 }
48 };
65a43f48 49 }
50}
51
8cf40f80 52sub clear : method {
53 my ($attr, $reader, $writer) = @_;
54 return sub { %{$reader->($_[0])} = () };
55}
56
65a43f48 57sub delete : method {
457dc4fb 58 my ($attr, $reader, $writer) = @_;
d1213e92 59 return sub {
60 my $hashref = $reader->(shift);
61 CORE::delete @{$hashref}{@_};
62 };
65a43f48 63}
64
651;
66
5431dff2 67__END__
68
69=pod
70
71=head1 NAME
72
73MooseX::AttributeHelpers::MethodProvider::Hash
74
75=head1 DESCRIPTION
76
77This is a role which provides the method generators for
78L<MooseX::AttributeHelpers::Collection::Hash>.
79
9a976497 80This role is composed from the
81L<MooseX::AttributeHelpers::Collection::ImmutableHash> role.
82
5431dff2 83=head1 METHODS
84
85=over 4
86
87=item B<meta>
88
89=back
90
91=head1 PROVIDED METHODS
92
93=over 4
94
95=item B<count>
96
97=item B<delete>
98
99=item B<empty>
100
8cf40f80 101=item B<clear>
102
5431dff2 103=item B<exists>
104
105=item B<get>
106
107=item B<keys>
108
109=item B<set>
110
111=item B<values>
112
9a976497 113=item B<kv>
114
5431dff2 115=back
116
117=head1 BUGS
118
119All complex software has bugs lurking in it, and this module is no
120exception. If you find a bug please either email me, or add the bug
121to cpan-RT.
122
123=head1 AUTHOR
124
125Stevan Little E<lt>stevan@iinteractive.comE<gt>
126
127=head1 COPYRIGHT AND LICENSE
128
99c62fb8 129Copyright 2007-2008 by Infinity Interactive, Inc.
5431dff2 130
131L<http://www.iinteractive.com>
132
133This library is free software; you can redistribute it and/or modify
134it under the same terms as Perl itself.
135
136=cut
137