6 Moose::Cookbook::Meta::Recipe4 - Adding a "table" attribute to the metaclass
10 package MyApp::Meta::Class;
12 extends 'Moose::Meta::Class';
21 In this recipe, we'll create a new metaclass which has a "table"
22 attribute. This metaclass is for classes associated with a DBMS table,
23 as one might do for an ORM.
25 In this example, the table name is just a string, but in a real ORM
26 the table might be an object describing the table.
30 This really is as simple as the recipe L</SYNOPSIS> shows. The trick
31 is getting your classes to use this metaclass, and providing some sort
32 of sugar for declaring the table. This is covered in
33 L<Moose::Cookbook::Extending::Recipe2>, which shows how to make a
34 module like C<Moose.pm> itself, with sugar like C<has_table()>.
36 =head2 Using this Metaclass in Practice
38 Accessing this new C<table> attribute is quite simple. Given a class
39 named C<MyApp::User>, we could simply write the following:
41 my $table = MyApp::User->meta->table;
43 As long as C<MyApp::User> has arranged to use C<MyApp::Meta::Class> as
44 its metaclass, this method call just works. If we want to be more
45 careful, we can check the metaclass's class:
47 $table = MyApp::User->meta->table
48 if MyApp::User->meta->isa('MyApp::Meta::Class');
52 Creating custom metaclass is trivial. Using it is a little harder, and
53 is covered in other recipes. We will also talk about applying traits
54 to a class metaclass, which is a more flexible and cooperative
59 L<Moose::Cookbook::Meta::Recipe5> - The "table" attribute implemented
62 L<Moose::Cookbook::Extending::Recipe2> - Acting like Moose.pm and
63 providing sugar Moose-style
67 Dave Rolsky E<lt>autarch@urth.orgE<gt>
69 =head1 COPYRIGHT AND LICENSE
71 Copyright 2006-2010 by Infinity Interactive, Inc.
73 L<http://www.iinteractive.com>
75 This library is free software; you can redistribute it and/or modify
76 it under the same terms as Perl itself.