meta_lookup needs to propagate downwards, if unspecified
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native.pm
CommitLineData
17e5e226 1package Moose::Meta::Attribute::Native;
e3c07b19 2
b5ae7c00 3use Class::Load qw(load_class);
4
cdf3cae6 5my @trait_names = qw(Bool Counter Number String Array Hash Code);
fafc8b9b 6
7for my $trait_name (@trait_names) {
c466e58f 8 my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name";
fafc8b9b 9 my $meta = Class::MOP::Class->initialize(
10 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
11 );
12 if ($meta->find_method_by_name('register_implementation')) {
13 my $class = $meta->name->register_implementation;
14 Moose->throw_error(
15 "An implementation for $trait_name already exists " .
16 "(found '$class' when trying to register '$trait_class')"
17 );
18 }
19 $meta->add_method(register_implementation => sub {
20 # resolve_metatrait_alias will load classes anyway, but throws away
21 # their error message; we WANT to die if there's a problem
b5ae7c00 22 load_class($trait_class);
fafc8b9b 23 return $trait_class;
24 });
25}
e3c07b19 26
e3c07b19 271;
28
ad46f524 29# ABSTRACT: Delegate to native Perl types
30
e3c07b19 31__END__
32
33=pod
34
e3c07b19 35=head1 SYNOPSIS
36
37 package MyClass;
38 use Moose;
e3c07b19 39
40 has 'mapping' => (
e132fd56 41 traits => ['Hash'],
42 is => 'rw',
43 isa => 'HashRef[Str]',
44 default => sub { {} },
45 handles => {
e3c07b19 46 exists_in_mapping => 'exists',
47 ids_in_mapping => 'keys',
48 get_mapping => 'get',
49 set_mapping => 'set',
cb562ad2 50 set_quantity => [ set => 'quantity' ],
e3c07b19 51 },
52 );
53
e3c07b19 54 my $obj = MyClass->new;
55 $obj->set_quantity(10); # quantity => 10
2420461c 56 $obj->set_mapping('foo', 4); # foo => 4
57 $obj->set_mapping('bar', 5); # bar => 5
58 $obj->set_mapping('baz', 6); # baz => 6
e3c07b19 59
2420461c 60 # prints 5
61 print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar');
e3c07b19 62
2420461c 63 # prints 'quantity, foo, bar, baz'
e3c07b19 64 print join ', ', $obj->ids_in_mapping;
65
66=head1 DESCRIPTION
67
e132fd56 68Native delegations allow you to delegate to native Perl data
e11cc42d 69structures as if they were objects. For example, in the L</SYNOPSIS> you can
e132fd56 70see a hash reference being treated as if it has methods named C<exists()>,
71C<keys()>, C<get()>, and C<set()>.
72
73The delegation methods (mostly) map to Perl builtins and operators. The return
74values of these delegations should be the same as the corresponding Perl
75operation. Any deviations will be explicitly documented.
76
77=head1 API
e3c07b19 78
e132fd56 79Native delegations are enabled by passing certain options to C<has> when
80creating an attribute.
e3c07b19 81
e132fd56 82=head2 traits
87b4e821 83
e132fd56 84To enable this feature, pass the appropriate name in the C<traits> array
85reference for the attribute. For example, to enable this feature for hash
86reference, we include C<'Hash'> in the list of traits.
87
88=head2 isa
89
90You will need to make sure that the attribute has an appropriate type. For
91example, to use this with a Hash you must specify that your attribute is some
92sort of C<HashRef>.
93
e3c07b19 94=head2 handles
95
e132fd56 96This is just like any other delegation, but only a hash reference is allowed
97when defining native delegations. The keys are the methods to be created in
98the class which contains the attribute. The values are the methods provided by
99the associated trait. Currying works the same way as it does with any other
100delegation.
e3c07b19 101
e132fd56 102See the docs for each native trait for details on what methods are available.
103
b79a1a50 104=head2 is
105
106Some traits provide a default C<is> for historical reasons. This behavior is
c33c3630 107deprecated, and you are strongly encouraged to provide a value. If you don't
ea867421 108plan to read and write the attribute value directly, not passing the C<is>
109option will prevent standard accessor generation.
b79a1a50 110
b6bf6592 111=head2 default or builder
112
b79a1a50 113Some traits provide a default C<default> for historical reasons. This behavior
b6bf6592 114is deprecated, and you are strongly encouraged to provide a default value or
115make the attribute required.
116
e132fd56 117=head1 TRAITS FOR NATIVE DELEGATIONS
e3c07b19 118
119=over
120
66cce11c 121=item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
e3c07b19 122
66cce11c 123 has 'queue' => (
e132fd56 124 traits => ['Array'],
125 is => 'ro',
126 isa => 'ArrayRef[Str]',
127 default => sub { [] },
128 handles => {
66cce11c 129 add_item => 'push',
130 next_item => 'shift',
39ab25ce 131 # ...
52e0d71f 132 }
133 );
134
66cce11c 135=item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
e3c07b19 136
66cce11c 137 has 'is_lit' => (
e132fd56 138 traits => ['Bool'],
139 is => 'ro',
140 isa => 'Bool',
141 default => 0,
142 handles => {
66cce11c 143 illuminate => 'set',
144 darken => 'unset',
145 flip_switch => 'toggle',
146 is_dark => 'not',
147 # ...
148 }
149 );
150
151=item L<Code|Moose::Meta::Attribute::Native::Trait::Code>
152
66cce11c 153 has 'callback' => (
e132fd56 154 traits => ['Code'],
155 is => 'ro',
156 isa => 'CodeRef',
157 default => sub {
158 sub {'called'}
159 },
160 handles => {
66cce11c 161 call => 'execute',
39ab25ce 162 # ...
52e0d71f 163 }
164 );
165
c466e58f 166=item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
e3c07b19 167
52e0d71f 168 has 'counter' => (
e132fd56 169 traits => ['Counter'],
170 is => 'ro',
171 isa => 'Num',
172 default => 0,
173 handles => {
52e0d71f 174 inc_counter => 'inc',
175 dec_counter => 'dec',
176 reset_counter => 'reset',
39ab25ce 177 # ...
52e0d71f 178 }
179 );
180
c466e58f 181=item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
e3c07b19 182
52e0d71f 183 has 'options' => (
e132fd56 184 traits => ['Hash'],
185 is => 'ro',
186 isa => 'HashRef[Str]',
187 default => sub { {} },
188 handles => {
9958cbe1 189 set_option => 'set',
190 get_option => 'get',
191 has_option => 'exists',
39ab25ce 192 # ...
52e0d71f 193 }
194 );
e3c07b19 195
66cce11c 196=item L<Number|Moose::Meta::Attribute::Native::Trait::Number>
e3c07b19 197
66cce11c 198 has 'integer' => (
e132fd56 199 traits => ['Number'],
200 is => 'ro',
201 isa => 'Int',
202 default => 5,
203 handles => {
66cce11c 204 set => 'set',
205 add => 'add',
206 sub => 'sub',
207 mul => 'mul',
208 div => 'div',
209 mod => 'mod',
210 abs => 'abs',
754a4833 211 # ...
212 }
52e0d71f 213 );
e3c07b19 214
66cce11c 215=item L<String|Moose::Meta::Attribute::Native::Trait::String>
b86a4688 216
66cce11c 217 has 'text' => (
e132fd56 218 traits => ['String'],
219 is => 'ro',
220 isa => 'Str',
221 default => q{},
222 handles => {
66cce11c 223 add_text => 'append',
224 replace_text => 'replace',
754a4833 225 # ...
226 }
b86a4688 227 );
228
e3c07b19 229=back
230
e132fd56 231=head1 COMPATIBILITY WITH MooseX::AttributeHelpers
232
233This feature used to be a separated CPAN distribution called
234L<MooseX::AttributeHelpers>.
235
236When the feature was incorporated into the Moose core, some of the API details
237were changed. The underlying capabilities are the same, but some details of
238the API were changed.
239
e3c07b19 240=head1 BUGS
241
d4048ef3 242See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 243
e3c07b19 244=cut