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