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