1131ae80c7b7433b6fe4e0f5c15fe7b1ad45fb29
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Collection / Hash.pm
1
2 package MooseX::AttributeHelpers::Collection::Hash;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.01';
7 our $AUTHORITY = 'cpan:STEVAN';
8
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
39     my $isa = $options->{isa}; 
40
41     unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
42         $isa = find_type_constraint($isa);        
43     }
44
45     ($isa->is_a_type_of('HashRef'))
46         || confess "The type constraint for a Hash ($options->{isa}) must be a subtype of HashRef";
47 }
48
49 no Moose;
50 no Moose::Util::TypeConstraints;
51
52 # register the alias ...
53 package Moose::Meta::Attribute::Custom::Collection::Hash;
54 sub register_implementation { 'MooseX::AttributeHelpers::Collection::Hash' }
55
56
57 1;
58
59 __END__
60
61 =pod
62
63 =head1 NAME
64
65 =head1 SYNOPSIS
66
67 =head1 DESCRIPTION
68
69 =head1 METHODS
70
71 =head1 BUGS
72
73 All complex software has bugs lurking in it, and this module is no 
74 exception. If you find a bug please either email me, or add the bug
75 to cpan-RT.
76
77 =head1 AUTHOR
78
79 Stevan Little E<lt>stevan@iinteractive.comE<gt>
80
81 =head1 COPYRIGHT AND LICENSE
82
83 Copyright 2007 by Infinity Interactive, Inc.
84
85 L<http://www.iinteractive.com>
86
87 This library is free software; you can redistribute it and/or modify
88 it under the same terms as Perl itself.
89
90 =cut