2 package Moose::Attribute::Native;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
8 my @trait_names = qw(Bool Counter Number String Array Hash);
10 for my $trait_name (@trait_names) {
11 my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name";
12 my $meta = Class::MOP::Class->initialize(
13 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
15 if ($meta->find_method_by_name('register_implementation')) {
16 my $class = $meta->name->register_implementation;
18 "An implementation for $trait_name already exists " .
19 "(found '$class' when trying to register '$trait_class')"
22 $meta->add_method(register_implementation => sub {
23 # resolve_metatrait_alias will load classes anyway, but throws away
24 # their error message; we WANT to die if there's a problem
25 Class::MOP::load_class($trait_class);
38 Moose::Attribute::Native - Extend your attribute interfaces
48 isa => 'HashRef[Str]',
49 default => sub { {} },
51 exists_in_mapping => 'exists',
52 ids_in_mapping => 'keys',
55 set_quantity => [ set => [ 'quantity' ] ],
62 my $obj = MyClass->new;
63 $obj->set_quantity(10); # quantity => 10
64 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
65 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
66 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
70 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
73 print join ', ', $obj->ids_in_mapping;
77 While L<Moose> attributes provide you with a way to name your accessors,
78 readers, writers, clearers and predicates, this library provides commonly
79 used attribute helper methods for more specific types of data.
81 As seen in the L</SYNOPSIS>, you specify the extension via the
82 C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>.
84 This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
85 very commonly used, so we moved it into core Moose. Since this gave us a chance
86 to change the interface, you will have to change your code or continue using
87 the L<MooseX::AttributeHelpers> extension.
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.
106 =item L<String|Moose::Meta::Attribute::Native::Trait::String>
108 Common methods for string operations.
110 =item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
112 Methods for incrementing and decrementing a counter attribute.
114 =item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
116 Common methods for boolean values.
118 =item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
120 Common methods for hash references.
122 =item L<ImmutableHash|Moose::Meta::Attribute::Native::Trait::ImmutableHash>
124 Common methods for inspecting hash references.
126 =item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
128 Common methods for array references.
130 =item L<List|Moose::Meta::Attribute::Native::Trait::List>
132 Common list methods for array references.
138 All complex software has bugs lurking in it, and this module is no
139 exception. If you find a bug please either email me, or add the bug
144 Stevan Little E<lt>stevan@iinteractive.comE<gt>
146 B<with contributions from:>
150 Paul (frodwith) Driver
154 Chris (perigrin) Prather
156 Robert (phaylon) Sedlacek
166 Florian (rafl) Ragwitz
172 =head1 COPYRIGHT AND LICENSE
174 Copyright 2007-2009 by Infinity Interactive, Inc.
176 L<http://www.iinteractive.com>
178 This library is free software; you can redistribute it and/or modify
179 it under the same terms as Perl itself.