Commit | Line | Data |
22d869ff |
1 | |
2 | package MooseX::AttributeHelpers::Collection::Hash; |
d26633fc |
3 | use Moose; |
22d869ff |
4 | |
5 | our $VERSION = '0.01'; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8c651099 |
8 | extends 'MooseX::AttributeHelpers::Collection'; |
d26633fc |
9 | |
8ba40fb0 |
10 | sub helper_type { 'HashRef' } |
11 | |
d26633fc |
12 | has '+method_constructors' => ( |
13 | default => sub { |
14 | return +{ |
15 | 'get' => sub { |
16 | my $attr = shift; |
17 | return sub { $attr->get_value($_[0])->{$_[1]} }; |
18 | }, |
19 | 'set' => sub { |
20 | my $attr = shift; |
8c651099 |
21 | if ($attr->has_container_type) { |
22 | my $container_type_constraint = $attr->container_type_constraint; |
23 | return sub { |
24 | ($container_type_constraint->check($_[2])) |
25 | || confess "Value $_[2] did not pass container type constraint"; |
26 | $attr->get_value($_[0])->{$_[1]} = $_[2] |
27 | }; |
28 | } |
29 | else { |
30 | return sub { $attr->get_value($_[0])->{$_[1]} = $_[2] }; |
31 | } |
d26633fc |
32 | }, |
8c651099 |
33 | 'keys' => sub { |
34 | my $attr = shift; |
35 | return sub { keys %{$attr->get_value($_[0])} }; |
36 | }, |
37 | 'values' => sub { |
38 | my $attr = shift; |
39 | return sub { values %{$attr->get_value($_[0])} }; |
40 | }, |
d26633fc |
41 | 'count' => sub { |
42 | my $attr = shift; |
43 | return sub { scalar keys %{$attr->get_value($_[0])} }; |
44 | }, |
45 | 'empty' => sub { |
46 | my $attr = shift; |
47 | return sub { scalar keys %{$attr->get_value($_[0])} ? 1 : 0 }; |
48 | } |
49 | } |
50 | } |
51 | ); |
52 | |
d26633fc |
53 | no Moose; |
54 | |
55 | # register the alias ... |
56 | package Moose::Meta::Attribute::Custom::Collection::Hash; |
57 | sub register_implementation { 'MooseX::AttributeHelpers::Collection::Hash' } |
58 | |
59 | |
22d869ff |
60 | 1; |
61 | |
62 | __END__ |
63 | |
64 | =pod |
65 | |
66 | =head1 NAME |
67 | |
68 | =head1 SYNOPSIS |
69 | |
70 | =head1 DESCRIPTION |
71 | |
72 | =head1 METHODS |
73 | |
74 | =head1 BUGS |
75 | |
76 | All complex software has bugs lurking in it, and this module is no |
77 | exception. If you find a bug please either email me, or add the bug |
78 | to cpan-RT. |
79 | |
80 | =head1 AUTHOR |
81 | |
82 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
83 | |
84 | =head1 COPYRIGHT AND LICENSE |
85 | |
86 | Copyright 2007 by Infinity Interactive, Inc. |
87 | |
88 | L<http://www.iinteractive.com> |
89 | |
90 | This library is free software; you can redistribute it and/or modify |
91 | it under the same terms as Perl itself. |
92 | |
93 | =cut |