1 package Moose::Cookbook::Meta::Recipe4;
3 # ABSTRACT: Adding a "table" attribute to the metaclass
12 package MyApp::Meta::Class;
14 extends 'Moose::Meta::Class';
23 In this recipe, we'll create a new metaclass which has a "table"
24 attribute. This metaclass is for classes associated with a DBMS table,
25 as one might do for an ORM.
27 In this example, the table name is just a string, but in a real ORM
28 the table might be an object describing the table.
32 This really is as simple as the recipe L</SYNOPSIS> shows. The trick
33 is getting your classes to use this metaclass, and providing some sort
34 of sugar for declaring the table. This is covered in
35 L<Moose::Cookbook::Extending::Recipe2>, which shows how to make a
36 module like C<Moose.pm> itself, with sugar like C<has_table()>.
38 =head2 Using this Metaclass in Practice
40 Accessing this new C<table> attribute is quite simple. Given a class
41 named C<MyApp::User>, we could simply write the following:
43 my $table = MyApp::User->meta->table;
45 As long as C<MyApp::User> has arranged to use C<MyApp::Meta::Class> as
46 its metaclass, this method call just works. If we want to be more
47 careful, we can check the metaclass's class:
49 $table = MyApp::User->meta->table
50 if MyApp::User->meta->isa('MyApp::Meta::Class');
54 Creating custom metaclass is trivial. Using it is a little harder, and
55 is covered in other recipes. We will also talk about applying traits
56 to a class metaclass, which is a more flexible and cooperative
61 L<Moose::Cookbook::Meta::Recipe5> - The "table" attribute implemented
64 L<Moose::Cookbook::Extending::Recipe2> - Acting like Moose.pm and
65 providing sugar Moose-style