1Moose::Cookbook::Meta::UTsaebrleC_oMnettraiMcboluoatsseesd:T:rPCaeoirotlk(b3Do)oocku:m:eMnettaat:i:oTnable_MetaclassTrait(3)
2
3
4

NAME

6       Moose::Cookbook::Meta::Table_MetaclassTrait - Adding a "table"
7       attribute as a metaclass trait
8

VERSION

10       version 2.2014
11

SYNOPSIS

13         # in lib/MyApp/Meta/Class/Trait/HasTable.pm
14         package MyApp::Meta::Class::Trait::HasTable;
15         use Moose::Role;
16         Moose::Util::meta_class_alias('HasTable');
17
18         has table => (
19             is  => 'rw',
20             isa => 'Str',
21         );
22
23         # in lib/MyApp/User.pm
24         package MyApp::User;
25         use Moose -traits => 'HasTable';
26
27         __PACKAGE__->meta->table('User');
28

DESCRIPTION

30       In this recipe, we'll create a class metaclass trait which has a
31       "table" attribute. This trait is for classes associated with a DBMS
32       table, as one might do for an ORM.
33
34       In this example, the table name is just a string, but in a real ORM the
35       table might be an object describing the table.
36

THE METACLASS TRAIT

38       This really is as simple as the recipe "SYNOPSIS" shows. The trick is
39       getting your classes to use this metaclass, and providing some sort of
40       sugar for declaring the table. This is covered in
41       Moose::Cookbook::Extending::Debugging_BaseClassRole, which shows how to
42       make a module like "Moose.pm" itself, with sugar like "has_table()".
43
44   Using this Metaclass Trait in Practice
45       Accessing this new "table" attribute is quite simple. Given a class
46       named "MyApp::User", we could simply write the following:
47
48         my $table = MyApp::User->meta->table;
49
50       As long as "MyApp::User" has arranged to apply the
51       "MyApp::Meta::Class::Trait::HasTable" to its metaclass, this method
52       call just works. If we want to be more careful, we can check that the
53       class metaclass object has a "table" method:
54
55         $table = MyApp::User->meta->table
56             if MyApp::User->meta->can('table');
57
58       In theory, this is not entirely correct, since the metaclass might be
59       getting its "table" method from a different trait. In practice, you are
60       unlikely to encounter this sort of problem.
61

RECIPE CAVEAT

63       This recipe doesn't work when you paste it all into a single file. This
64       is because the "use Moose -traits => 'HasTable';" line ends up being
65       executed before the "table" attribute is defined.
66
67       When the two packages are separate files, this just works.
68

SEE ALSO

70       Moose::Cookbook::Meta::Labeled_AttributeTrait - Labels implemented via
71       attribute traits =pod
72

AUTHORS

74       •   Stevan Little <stevan@cpan.org>
75
76       •   Dave Rolsky <autarch@urth.org>
77
78       •   Jesse Luehrs <doy@cpan.org>
79
80       •   Shawn M Moore <sartak@cpan.org>
81
82       •   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
83
84       •   Karen Etheridge <ether@cpan.org>
85
86       •   Florian Ragwitz <rafl@debian.org>
87
88       •   Hans Dieter Pearcey <hdp@cpan.org>
89
90       •   Chris Prather <chris@prather.org>
91
92       •   Matt S Trout <mstrout@cpan.org>
93
95       This software is copyright (c) 2006 by Infinity Interactive, Inc.
96
97       This is free software; you can redistribute it and/or modify it under
98       the same terms as the Perl 5 programming language system itself.
99
100
101
102perl v5.32.1                    Mo2o0s2e1:-:0C1o-o2k7book::Meta::Table_MetaclassTrait(3)
Impressum