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