Removed Meta recipe2 (an attribute metaclass)
[gitmo/Moose.git] / lib / Moose / Cookbook / Meta / Recipe4.pod
CommitLineData
daa0fd7d 1package Moose::Cookbook::Meta::Recipe4;
3f002851 2
daa0fd7d 3# ABSTRACT: Adding a "table" attribute to the metaclass
4
5__END__
3f002851 6
3f002851 7
daa0fd7d 8=pod
3f002851 9
10=head1 SYNOPSIS
11
12 package MyApp::Meta::Class;
13 use Moose;
14 extends 'Moose::Meta::Class';
15
6a7e3999 16 has table => (
17 is => 'rw',
18 isa => 'Str',
19 );
3f002851 20
21=head1 DESCRIPTION
22
23In this recipe, we'll create a new metaclass which has a "table"
24attribute. This metaclass is for classes associated with a DBMS table,
25as one might do for an ORM.
26
27In this example, the table name is just a string, but in a real ORM
28the table might be an object describing the table.
29
30=head1 THE METACLASS
31
ccea63ce 32This really is as simple as the recipe L</SYNOPSIS> shows. The trick
33is getting your classes to use this metaclass, and providing some sort
34of sugar for declaring the table. This is covered in
35L<Moose::Cookbook::Extending::Recipe2>, which shows how to make a
6fa0a13f 36module like C<Moose.pm> itself, with sugar like C<has_table()>.
3f002851 37
c5b9daec 38=head2 Using this Metaclass in Practice
3f002851 39
ccea63ce 40Accessing this new C<table> attribute is quite simple. Given a class
41named C<MyApp::User>, we could simply write the following:
3f002851 42
6a7e3999 43 my $table = MyApp::User->meta->table;
3f002851 44
ccea63ce 45As long as C<MyApp::User> has arranged to use C<MyApp::Meta::Class> as
46its metaclass, this method call just works. If we want to be more
47careful, we can check the metaclass's class:
48
49 $table = MyApp::User->meta->table
50 if MyApp::User->meta->isa('MyApp::Meta::Class');
51
52=head1 CONCLUSION
53
54Creating custom metaclass is trivial. Using it is a little harder, and
55is covered in other recipes. We will also talk about applying traits
56to a class metaclass, which is a more flexible and cooperative
57implementation.
3f002851 58
59=head1 SEE ALSO
60
61L<Moose::Cookbook::Meta::Recipe5> - The "table" attribute implemented
c5b9daec 62as a metaclass trait
63
64L<Moose::Cookbook::Extending::Recipe2> - Acting like Moose.pm and
65providing sugar Moose-style
3f002851 66
3f002851 67=pod