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