1 package Moose::Meta::Attribute::Native;
3 our $VERSION = '0.89_02';
4 $VERSION = eval $VERSION;
5 our $AUTHORITY = 'cpan:STEVAN';
7 my @trait_names = qw(Bool Counter Number String Array Hash Code);
9 for my $trait_name (@trait_names) {
10 my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name";
11 my $meta = Class::MOP::Class->initialize(
12 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
14 if ($meta->find_method_by_name('register_implementation')) {
15 my $class = $meta->name->register_implementation;
17 "An implementation for $trait_name already exists " .
18 "(found '$class' when trying to register '$trait_class')"
21 $meta->add_method(register_implementation => sub {
22 # resolve_metatrait_alias will load classes anyway, but throws away
23 # their error message; we WANT to die if there's a problem
24 Class::MOP::load_class($trait_class);
37 Moose::Meta::Attribute::Native - Extend your attribute interfaces
47 isa => 'HashRef[Str]',
48 default => sub { {} },
50 exists_in_mapping => 'exists',
51 ids_in_mapping => 'keys',
54 set_quantity => [ set => 'quantity' ],
61 my $obj = MyClass->new;
62 $obj->set_quantity(10); # quantity => 10
63 $obj->set_mapping('foo', 4); # foo => 4
64 $obj->set_mapping('bar', 5); # bar => 5
65 $obj->set_mapping('baz', 6); # baz => 6
69 print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar');
71 # prints 'quantity, foo, bar, baz'
72 print join ', ', $obj->ids_in_mapping;
76 While L<Moose> attributes provide a way to name your accessors, readers,
77 writers, clearers and predicates, this set of traits provides commonly
78 used attribute helper methods for more specific types of data.
80 As seen in the L</SYNOPSIS>, you specify the data structure via the
81 C<trait> parameter. Available traits are below; see L</METHOD PROVIDERS>.
83 This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
84 very commonly used, so we moved it into core Moose. Since this gave us a chance
85 to change the interface, you will have to change your code or continue using
86 the L<MooseX::AttributeHelpers> extension. L<MooseX::AttributeHelpers> should
93 This is like C<< handles >> in L<Moose/has>, but only HASH references are
94 allowed. Keys are method names that you want installed locally, and values are
95 methods from the method providers (below). Currying with delegated methods
96 works normally for C<< handles >>.
98 =head1 METHOD PROVIDERS
102 =item L<Number|Moose::Meta::Attribute::Native::Trait::Number>
104 Common numerical operations.
107 traits => ['Number'],
122 =item L<String|Moose::Meta::Attribute::Native::Trait::String>
124 Common methods for string operations.
127 traits => ['String'],
132 add_text => 'append',
133 replace_text => 'replace',
137 =item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
139 Methods for incrementing and decrementing a counter attribute.
142 traits => ['Counter'],
147 inc_counter => 'inc',
148 dec_counter => 'dec',
149 reset_counter => 'reset',
153 =item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
155 Common methods for boolean values.
165 flip_switch => 'toggle',
170 =item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
172 Common methods for hash references.
177 isa => 'HashRef[Str]',
178 default => sub { {} },
182 has_option => 'exists',
186 =item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
188 Common methods for array references.
193 isa => 'ArrayRef[Str]',
194 default => sub { [] },
197 next_item => 'shift',
201 =item L<Code|Moose::Meta::Attribute::Native::Trait::Code>
203 Common methods for code references.
209 default => sub { sub { 'called' } },
219 All complex software has bugs lurking in it, and this module is no
220 exception. If you find a bug please either email me, or add the bug
225 Stevan Little E<lt>stevan@iinteractive.comE<gt>
227 B<with contributions from:>
231 Paul (frodwith) Driver
235 Chris (perigrin) Prather
237 Robert (phaylon) Sedlacek
247 Florian (rafl) Ragwitz
253 =head1 COPYRIGHT AND LICENSE
255 Copyright 2007-2009 by Infinity Interactive, Inc.
257 L<http://www.iinteractive.com>
259 This library is free software; you can redistribute it and/or modify
260 it under the same terms as Perl itself.