bump copyright year to 2010
[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
5377c260 28This recipe takes the metaclass table attribute from
19320607 29L<Moose::Cookbook::Meta::Recipe4> and implements it as a metaclass
5377c260 30trait. Traits are just roles, as we saw in
31L<Moose::Cookbook::Meta::Recipe3>.
c5b9daec 32
33The advantage of using traits is that it's easy to combine multiple
5377c260 34traits, whereas combining multiple metaclass subclasses requires
35creating yet another subclass. With traits, Moose takes care of
36applying them to your metaclass.
c5b9daec 37
38=head2 Using this Metaclass Trait in Practice
39
40Once this trait has been applied to a metaclass, it looks exactly like
41the example we saw in L<Moose::Cookbook::Meta::Recipe4>:
42
6a7e3999 43 my $table = MyApp::User->meta->table;
c5b9daec 44
5377c260 45 # the safe version
46 $table = MyApp::User->meta->table
47 if MyApp::User->meta->meta->can('does')
48 and MyApp::User->meta->meta->does('MyApp::Meta::Class');
49
50The safe version is a little complicated. We have to check that the
51metaclass object's metaclass has a C<does> method, in which case we
52can ask if the the metaclass does a given role.
53
54It's simpler to just write:
55
56 $table = MyApp::User->meta->table
57 if MyApp::User->meta->can('table');
58
59In theory, this is a little less correct, since the metaclass might be
60getting its C<table> method from a I<different> role. In practice, you
61are unlikely to encounter this sort of problem.
62
c5b9daec 63=head1 SEE ALSO
64
65L<Moose::Cookbook::Meta::Recipe3> - Labels implemented via attribute
66traits
67
68L<Moose::Cookbook::Meta::Recipe4> - Adding a "table" attribute to the
69metaclass
70
71=head1 AUTHOR
72
73Dave Rolsky E<lt>autarch@urth.orgE<gt>
74
75=head1 COPYRIGHT AND LICENSE
76
7e0492d3 77Copyright 2006-2010 by Infinity Interactive, Inc.
c5b9daec 78
79L<http://www.iinteractive.com>
80
81This library is free software; you can redistribute it and/or modify
82it under the same terms as Perl itself.
83
84=pod