Rename Meta::Recipe5 to Meta::Table_MetaclassTrait
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Recipe4.pod
CommitLineData
daa0fd7d 1package Moose::Cookbook::Extending::Recipe4;
6fa0a13f 2
daa0fd7d 3# ABSTRACT: Acting like Moose.pm and providing sugar Moose-style
4
5__END__
6fa0a13f 6
6fa0a13f 7
daa0fd7d 8=pod
6fa0a13f 9
10=head1 SYNOPSIS
11
12 package MyApp::Mooseish;
13
554b7648 14 use Moose::Exporter;
6fa0a13f 15
aedcb7d9 16 Moose::Exporter->setup_import_methods(
9e0a5711 17 with_meta => ['has_table'],
18 class_metaroles => {
19 class => ['MyApp::Meta::Class::Trait::HasTable'],
20 },
554b7648 21 );
6fa0a13f 22
6fa0a13f 23 sub has_table {
d5447d26 24 my $meta = shift;
25 $meta->table(shift);
6fa0a13f 26 }
27
9e0a5711 28 package MyApp::Meta::Class::Trait::HasTable;
29 use Moose::Role;
c79239a2 30
9e0a5711 31 has table => (
32 is => 'rw',
33 isa => 'Str',
34 );
c79239a2 35
6fa0a13f 36=head1 DESCRIPTION
37
554b7648 38This recipe expands on the use of L<Moose::Exporter> we saw in
9e0a5711 39L<Moose::Cookbook::Extending::Recipe1> and the class metaclass trait we saw in
826230c2 40L<Moose::Cookbook::Meta::Table_MetaclassTrait>. In this example we provide our
41own metaclass trait, and we also export a C<has_table> sugar function.
6fa0a13f 42
d5447d26 43The C<with_meta> parameter specifies a list of functions that should
554b7648 44be wrapped before exporting. The wrapper simply ensures that the
47820b5c 45importing package's appropriate metaclass object is the first argument
46to the function, so we can do C<S<my $meta = shift;>>.
6fa0a13f 47
554b7648 48See the L<Moose::Exporter> docs for more details on its API.
6fa0a13f 49
554b7648 50=head1 USING MyApp::Mooseish
6fa0a13f 51
52The purpose of all this code is to provide a Moose-like
53interface. Here's what it would look like in actual use:
54
55 package MyApp::User;
56
9e0a5711 57 use namespace::autoclean;
58
59 use Moose;
6fa0a13f 60 use MyApp::Mooseish;
61
62 has_table 'User';
63
554b7648 64 has 'username' => ( is => 'ro' );
65 has 'password' => ( is => 'ro' );
6fa0a13f 66
67 sub login { ... }
68
c09fae0b 69=head1 CONCLUSION
70
71Providing sugar functions can make your extension look much more
72Moose-ish. See L<Fey::ORM> for a more extensive example.
73
c79239a2 74=begin testing
75
76
77{
78 package MyApp::User;
79
80 MyApp::Mooseish->import;
81
82 has_table( 'User' );
83
84 has( 'username' => ( is => 'ro' ) );
85 has( 'password' => ( is => 'ro' ) );
86
87 sub login { }
88}
89
90isa_ok( MyApp::User->meta, 'MyApp::Meta::Class' );
91is( MyApp::User->meta->table, 'User',
92 'MyApp::User->meta->table returns User' );
93ok( MyApp::User->can('username'),
94 'MyApp::User has username method' );
95
96=end testing
97
6fa0a13f 98=pod