Tweaks for Classes document
[gitmo/Moose.git] / lib / Moose / Cookbook / Meta / Recipe5.pod
CommitLineData
c5b9daec 1
2=pod
3
4=head1 NAME
5
6Moose::Cookbook::Meta::Recipe5 - The "table" attribute as a metaclass trait
7
8=head1 SYNOPSIS
9
10 package MyApp::Meta::Class::Trait::HasTable;
11 use Moose::Role;
12
6a7e3999 13 has table => (
14 is => 'rw',
15 isa => 'Str',
16 );
c5b9daec 17
18 package Moose::Meta::Class::Custom::Trait::HasTable;
19 sub register_implementation { 'MyApp::Meta::Class::Trait::HasTable' }
20
21 package MyApp::User;
22 use Moose -traits => 'HasTable';
23
fe015af9 24 __PACKAGE__->meta->table('User');
c5b9daec 25
26=head1 DESCRIPTION
27
28This recipe takes the metaclass table attribute and reimplements it as
29a metaclass trait. Traits are just roles that Moose applies to
30something for you. In this case, that "something" is the class's
31metaclass object.
32
33The advantage of using traits is that it's easy to combine multiple
34traits, whereas combining multiple metaclasses can be tricky (which
35subclasses which?).
36
37The disadvantage is that it's not easy to combine a trait with some
38sort of sugar (like our notional C<has_table> sugar).
39
40=head2 Using this Metaclass Trait in Practice
41
42Once this trait has been applied to a metaclass, it looks exactly like
43the example we saw in L<Moose::Cookbook::Meta::Recipe4>:
44
6a7e3999 45 my $table = MyApp::User->meta->table;
c5b9daec 46
47=head1 SEE ALSO
48
49L<Moose::Cookbook::Meta::Recipe3> - Labels implemented via attribute
50traits
51
52L<Moose::Cookbook::Meta::Recipe4> - Adding a "table" attribute to the
53metaclass
54
55=head1 AUTHOR
56
57Dave Rolsky E<lt>autarch@urth.orgE<gt>
58
59=head1 COPYRIGHT AND LICENSE
60
61Copyright 2006-2008 by Infinity Interactive, Inc.
62
63L<http://www.iinteractive.com>
64
65This library is free software; you can redistribute it and/or modify
66it under the same terms as Perl itself.
67
68=pod