it's a role, not a class
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Trait / Native / MethodProvider / ImmutableHash.pm
CommitLineData
a40b446a 1package Moose::Meta::Attribute::Trait::Native::MethodProvider::ImmutableHash;
e3c07b19 2use Moose::Role;
3
96539d20 4our $VERSION = '0.87';
e3c07b19 5$VERSION = eval $VERSION;
6our $AUTHORITY = 'cpan:STEVAN';
7
8sub exists : method {
046c8b5e 9 my ( $attr, $reader, $writer ) = @_;
10 return sub { CORE::exists $reader->( $_[0] )->{ $_[1] } ? 1 : 0 };
e3c07b19 11}
12
13sub defined : method {
046c8b5e 14 my ( $attr, $reader, $writer ) = @_;
15 return sub { CORE::defined $reader->( $_[0] )->{ $_[1] } ? 1 : 0 };
e3c07b19 16}
17
18sub get : method {
046c8b5e 19 my ( $attr, $reader, $writer ) = @_;
e3c07b19 20 return sub {
21 if ( @_ == 2 ) {
046c8b5e 22 $reader->( $_[0] )->{ $_[1] };
23 }
24 else {
e3c07b19 25 my ( $self, @keys ) = @_;
046c8b5e 26 @{ $reader->($self) }{@keys};
e3c07b19 27 }
28 };
29}
30
31sub keys : method {
046c8b5e 32 my ( $attr, $reader, $writer ) = @_;
33 return sub { CORE::keys %{ $reader->( $_[0] ) } };
e3c07b19 34}
35
36sub values : method {
046c8b5e 37 my ( $attr, $reader, $writer ) = @_;
38 return sub { CORE::values %{ $reader->( $_[0] ) } };
e3c07b19 39}
40
41sub kv : method {
046c8b5e 42 my ( $attr, $reader, $writer ) = @_;
e3c07b19 43 return sub {
046c8b5e 44 my $h = $reader->( $_[0] );
45 map { [ $_, $h->{$_} ] } CORE::keys %{$h};
e3c07b19 46 };
47}
48
49sub elements : method {
046c8b5e 50 my ( $attr, $reader, $writer ) = @_;
e3c07b19 51 return sub {
046c8b5e 52 my $h = $reader->( $_[0] );
53 map { $_, $h->{$_} } CORE::keys %{$h};
e3c07b19 54 };
55}
56
57sub count : method {
046c8b5e 58 my ( $attr, $reader, $writer ) = @_;
59 return sub { scalar CORE::keys %{ $reader->( $_[0] ) } };
e3c07b19 60}
61
62sub empty : method {
046c8b5e 63 my ( $attr, $reader, $writer ) = @_;
64 return sub { scalar CORE::keys %{ $reader->( $_[0] ) } ? 1 : 0 };
e3c07b19 65}
66
671;
68
69__END__
70
71=pod
72
73=head1 NAME
74
a40b446a 75Moose::Meta::Attribute::Trait::Native::MethodProvider::ImmutableHash
e3c07b19 76
77=head1 DESCRIPTION
78
79This is a role which provides the method generators for
a40b446a 80L<Moose::Meta::Attribute::Trait::Native::ImmutableHash>.
e3c07b19 81
82=head1 METHODS
83
84=over 4
85
86=item B<meta>
87
88=back
89
90=head1 PROVIDED METHODS
91
92=over 4
93
94=item B<count>
95
96Returns the number of elements in the list.
97
98=item B<empty>
99
100If the list is populated, returns true. Otherwise, returns false.
101
102=item B<exists>
103
104Returns true if the given key is present in the hash
105
106=item B<defined>
107
108Returns true if the value of a given key is defined
109
110=item B<get>
111
112Returns an element of the hash by its key.
113
114=item B<keys>
115
116Returns the list of keys in the hash.
117
118=item B<values>
119
120Returns the list of values in the hash.
121
122=item B<kv>
123
124Returns the key, value pairs in the hash as array references
125
126=item B<elements>
127
128Returns the key, value pairs in the hash as a flattened list
129
130=back
131
132=head1 BUGS
133
134All complex software has bugs lurking in it, and this module is no
135exception. If you find a bug please either email me, or add the bug
136to cpan-RT.
137
138=head1 AUTHOR
139
140Stevan Little E<lt>stevan@iinteractive.comE<gt>
141
142=head1 COPYRIGHT AND LICENSE
143
144Copyright 2007-2009 by Infinity Interactive, Inc.
145
146L<http://www.iinteractive.com>
147
148This library is free software; you can redistribute it and/or modify
149it under the same terms as Perl itself.
150
151=cut
152