lazily load the native trait accessor roles
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Hash.pm
CommitLineData
e3c07b19 1
c466e58f 2package Moose::Meta::Attribute::Native::Trait::Hash;
e3c07b19 3use Moose::Role;
4
245478d5 5our $VERSION = '1.19';
e3c07b19 6$VERSION = eval $VERSION;
7our $AUTHORITY = 'cpan:STEVAN';
8
c466e58f 9with 'Moose::Meta::Attribute::Native::Trait';
e3c07b19 10
2e069f5a 11sub _helper_type { 'HashRef' }
e3c07b19 12
13no Moose::Role;
14
e3c07b19 151;
16
17__END__
18
19=pod
20
21=head1 NAME
22
2420461c 23Moose::Meta::Attribute::Native::Trait::Hash - Helper trait for HashRef attributes
e3c07b19 24
25=head1 SYNOPSIS
26
27 package Stuff;
28 use Moose;
e3c07b19 29
30 has 'options' => (
96fd0bec 31 traits => ['Hash'],
e3c07b19 32 is => 'ro',
33 isa => 'HashRef[Str]',
34 default => sub { {} },
5f3663b2 35 handles => {
af44c00c 36 set_option => 'set',
37 get_option => 'get',
bb023e15 38 has_no_options => 'is_empty',
af44c00c 39 num_options => 'count',
40 delete_option => 'delete',
43852ddb 41 option_pairs => 'kv',
9610c1d2 42 },
e3c07b19 43 );
44
45=head1 DESCRIPTION
46
7795e4de 47This trait provides native delegation methods for array references.
96fd0bec 48
49=head1 PROVIDED METHODS
50
96fd0bec 51=over 4
52
85592815 53=item B<get($key, $key2, $key3...)>
96fd0bec 54
85592815 55Returns values from the hash.
96fd0bec 56
e132fd56 57In list context it returns a list of values in the hash for the given keys. In
58scalar context it returns the value for the last key specified.
59
60This method requires at least one argument.
96fd0bec 61
9d010f57 62=item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)>
85592815 63
e132fd56 64Sets the elements in the hash to the given values. It returns the new values
65set for each key, in the same order as the keys passed to the method.
66
67This method requires at least two arguments, and expects an even number of
68arguments.
96fd0bec 69
9d010f57 70=item B<delete($key, $key2, $key3...)>
96fd0bec 71
9d010f57 72Removes the elements with the given keys.
96fd0bec 73
e132fd56 74In list context it returns a list of values in the hash for the deleted
75keys. In scalar context it returns the value for the last key specified.
76
8396a437 77=item B<keys>
96fd0bec 78
8396a437 79Returns the list of keys in the hash.
96fd0bec 80
e132fd56 81This method does not accept any arguments.
82
8396a437 83=item B<exists($key)>
96fd0bec 84
8396a437 85Returns true if the given key is present in the hash.
96fd0bec 86
e132fd56 87This method requires a single argument.
88
8396a437 89=item B<defined($key)>
96fd0bec 90
8396a437 91Returns true if the value of a given key is defined.
96fd0bec 92
e132fd56 93This method requires a single argument.
94
96fd0bec 95=item B<values>
96
97Returns the list of values in the hash.
98
e132fd56 99This method does not accept any arguments.
100
96fd0bec 101=item B<kv>
102
8396a437 103Returns the key/value pairs in the hash as an array of array references.
104
105 for my $pair ( $object->options->pairs ) {
106 print "$pair->[0] = $pair->[1]\n";
107 }
96fd0bec 108
e132fd56 109This method does not accept any arguments.
110
96fd0bec 111=item B<elements>
112
8396a437 113Returns the key/value pairs in the hash as a flattened list..
96fd0bec 114
e132fd56 115This method does not accept any arguments.
116
8396a437 117=item B<clear>
96fd0bec 118
8396a437 119Resets the hash to an empty value, like C<%hash = ()>.
96fd0bec 120
e132fd56 121This method does not accept any arguments.
122
8396a437 123=item B<count>
96fd0bec 124
39e17fef 125Returns the number of elements in the hash. Also useful for not empty:
126C<< has_options => 'count' >>.
96fd0bec 127
e132fd56 128This method does not accept any arguments.
129
bb023e15 130=item B<is_empty>
96fd0bec 131
8396a437 132If the hash is populated, returns false. Otherwise, returns true.
96fd0bec 133
e132fd56 134This method does not accept any arguments.
135
136=item B<accessor($key)>
137
138=item B<accessor($key, $value)>
96fd0bec 139
8396a437 140If passed one argument, returns the value of the specified key. If passed two
141arguments, sets the value of the specified key.
96fd0bec 142
e132fd56 143When called as a setter, this method returns the value that was set.
144
96fd0bec 145=back
e3c07b19 146
147=head1 METHODS
148
149=over 4
150
151=item B<meta>
152
e3c07b19 153=back
154
155=head1 BUGS
156
d4048ef3 157See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 158
159=head1 AUTHOR
160
161Stevan Little E<lt>stevan@iinteractive.comE<gt>
162
163=head1 COPYRIGHT AND LICENSE
164
165Copyright 2007-2009 by Infinity Interactive, Inc.
166
167L<http://www.iinteractive.com>
168
169This library is free software; you can redistribute it and/or modify
170it under the same terms as Perl itself.
171
172=cut