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