Got inlining for hashes working.
[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.14';
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 use Moose::Meta::Attribute::Native::MethodProvider::Hash;
10
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
24 with 'Moose::Meta::Attribute::Native::Trait';
25
26 has 'method_provider' => (
27     is        => 'ro',
28     isa       => 'ClassName',
29     predicate => 'has_method_provider',
30     default   => 'Moose::Meta::Attribute::Native::MethodProvider::Hash'
31 );
32
33 sub _helper_type { 'HashRef' }
34
35 no Moose::Role;
36
37 1;
38
39 __END__
40
41 =pod
42
43 =head1 NAME
44
45 Moose::Meta::Attribute::Native::Trait::Hash - Helper trait for HashRef attributes
46
47 =head1 SYNOPSIS
48
49   package Stuff;
50   use Moose;
51
52   has 'options' => (
53       traits    => ['Hash'],
54       is        => 'ro',
55       isa       => 'HashRef[Str]',
56       default   => sub { {} },
57       handles   => {
58           set_option     => 'set',
59           get_option     => 'get',
60           has_no_options => 'is_empty',
61           num_options    => 'count',
62           delete_option  => 'delete',
63           option_pairs   => 'kv',
64       },
65   );
66
67 =head1 DESCRIPTION
68
69 This module provides a Hash attribute which provides a number of
70 hash-like operations.
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
79 =item B<get($key, $key2, $key3...)>
80
81 Returns values from the hash.
82
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.
85
86 =item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)>
87
88 Sets the elements in the hash to the given values.
89
90 =item B<delete($key, $key2, $key3...)>
91
92 Removes the elements with the given keys.
93
94 =item B<keys>
95
96 Returns the list of keys in the hash.
97
98 =item B<exists($key)>
99
100 Returns true if the given key is present in the hash.
101
102 =item B<defined($key)>
103
104 Returns true if the value of a given key is defined.
105
106 =item B<values>
107
108 Returns the list of values in the hash.
109
110 =item B<kv>
111
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   }
117
118 =item B<elements>
119
120 Returns the key/value pairs in the hash as a flattened list..
121
122 =item B<clear>
123
124 Resets the hash to an empty value, like C<%hash = ()>.
125
126 =item B<count>
127
128 Returns the number of elements in the hash. Also useful for not empty: 
129 C<< has_options => 'count' >>.
130
131 =item B<is_empty>
132
133 If the hash is populated, returns false. Otherwise, returns true.
134
135 =item B<accessor>
136
137 If passed one argument, returns the value of the specified key. If passed two
138 arguments, sets the value of the specified key.
139
140 =back
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
152 =back
153
154 =head1 BUGS
155
156 See L<Moose/BUGS> for details on reporting bugs.
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