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