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