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