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