Rename AttributeHelpers.pm to Attribute/Native.pm
[gitmo/Moose.git] / lib / Moose / Attribute / Native.pm
1
2 package Moose::Attribute::Native;
3
4 our $VERSION   = '0.87';
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 my @trait_names = qw(Bool Counter Number String Array Hash);
9
10 for my $trait_name (@trait_names) {
11     my $trait_class = "Moose::Meta::Attribute::Trait::Native::$trait_name";
12     my $meta = Class::MOP::Class->initialize(
13         "Moose::Meta::Attribute::Custom::Trait::$trait_name"
14     );
15     if ($meta->find_method_by_name('register_implementation')) {
16         my $class = $meta->name->register_implementation;
17         Moose->throw_error(
18             "An implementation for $trait_name already exists " .
19             "(found '$class' when trying to register '$trait_class')"
20         );
21     }
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);
26         return $trait_class;
27     });
28 }
29
30 1;
31
32 __END__
33
34 =pod
35
36 =head1 NAME
37
38 Moose::Attribute::Native - Extend your attribute interfaces
39
40 =head1 SYNOPSIS
41
42   package MyClass;
43   use Moose;
44   use Moose::Attribute::Native;
45
46   has 'mapping' => (
47       traits    => [ 'Hash' ],
48       is        => 'rw',
49       isa       => 'HashRef[Str]',
50       default   => sub { {} },
51       handles   => {
52           exists_in_mapping => 'exists',
53           ids_in_mapping    => 'keys',
54           get_mapping       => 'get',
55           set_mapping       => 'set',
56           set_quantity      => [ set => [ 'quantity' ] ],
57       },
58   );
59
60
61   # ...
62
63   my $obj = MyClass->new;
64   $obj->set_quantity(10);      # quantity => 10
65   $obj->set_mapping(4, 'foo'); # 4 => 'foo'
66   $obj->set_mapping(5, 'bar'); # 5 => 'bar'
67   $obj->set_mapping(6, 'baz'); # 6 => 'baz'
68
69
70   # prints 'bar'
71   print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
72
73   # prints '4, 5, 6'
74   print join ', ', $obj->ids_in_mapping;
75
76 =head1 DESCRIPTION
77
78 While L<Moose> attributes provide you with a way to name your accessors,
79 readers, writers, clearers and predicates, this library provides commonly
80 used attribute helper methods for more specific types of data.
81
82 As seen in the L</SYNOPSIS>, you specify the extension via the
83 C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>.
84
85 This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
86 very commonly used, so we moved it into core Moose. Since this gave us a chance
87 to change the interface, you will have to change your code or continue using
88 the L<MooseX::AttributeHelpers> extension.
89
90 =head1 PARAMETERS
91
92 =head2 handles
93
94 This is like C<< handles >> in L<Moose/has>, but only HASH references are
95 allowed.  Keys are method names that you want installed locally, and values are
96 methods from the method providers (below).  Currying with delegated methods
97 works normally for C<< handles >>.
98
99 =head1 METHOD PROVIDERS
100
101 =over
102
103 =item L<Number|Moose::Meta::Attribute::Trait::Native::Number>
104
105 Common numerical operations.
106
107 =item L<String|Moose::Meta::Attribute::Trait::Native::String>
108
109 Common methods for string operations.
110
111 =item L<Counter|Moose::Meta::Attribute::Trait::Native::Counter>
112
113 Methods for incrementing and decrementing a counter attribute.
114
115 =item L<Bool|Moose::Meta::Attribute::Trait::Native::Bool>
116
117 Common methods for boolean values.
118
119 =item L<Hash|Moose::Meta::Attribute::Trait::Native::Hash>
120
121 Common methods for hash references.
122
123 =item L<ImmutableHash|Moose::Meta::Attribute::Trait::Native::ImmutableHash>
124
125 Common methods for inspecting hash references.
126
127 =item L<Array|Moose::Meta::Attribute::Trait::Native::Array>
128
129 Common methods for array references.
130
131 =item L<List|Moose::Meta::Attribute::Trait::Native::List>
132
133 Common list methods for array references.
134
135 =back
136
137 =head1 BUGS
138
139 All complex software has bugs lurking in it, and this module is no
140 exception. If you find a bug please either email me, or add the bug
141 to cpan-RT.
142
143 =head1 AUTHOR
144
145 Stevan Little E<lt>stevan@iinteractive.comE<gt>
146
147 B<with contributions from:>
148
149 Robert (rlb3) Boone
150
151 Paul (frodwith) Driver
152
153 Shawn (Sartak) Moore
154
155 Chris (perigrin) Prather
156
157 Robert (phaylon) Sedlacek
158
159 Tom (dec) Lanyon
160
161 Yuval Kogman
162
163 Jason May
164
165 Cory (gphat) Watson
166
167 Florian (rafl) Ragwitz
168
169 Evan Carroll
170
171 Jesse (doy) Luehrs
172
173 =head1 COPYRIGHT AND LICENSE
174
175 Copyright 2007-2009 by Infinity Interactive, Inc.
176
177 L<http://www.iinteractive.com>
178
179 This library is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself.
181
182 =cut