Dzil-ize all the .pod files so they can be pod-woven
[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
6fa0a13f 14 use Moose ();
554b7648 15 use Moose::Exporter;
6fa0a13f 16
aedcb7d9 17 Moose::Exporter->setup_import_methods(
d5447d26 18 with_meta => ['has_table'],
19 also => 'Moose',
554b7648 20 );
6fa0a13f 21
554b7648 22 sub init_meta {
23 shift;
a8de959b 24 return Moose->init_meta( @_, metaclass => 'MyApp::Meta::Class' );
6fa0a13f 25 }
26
27 sub has_table {
d5447d26 28 my $meta = shift;
29 $meta->table(shift);
6fa0a13f 30 }
31
c79239a2 32 package MyApp::Meta::Class;
33 use Moose;
34
35 extends 'Moose::Meta::Class';
36
37 has 'table' => ( is => 'rw' );
38
6fa0a13f 39=head1 DESCRIPTION
40
554b7648 41This recipe expands on the use of L<Moose::Exporter> we saw in
9a8b19be 42L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own
43object base class, we provide our own metaclass class, and we also
c09fae0b 44export a C<has_table> sugar function.
6fa0a13f 45
46Given the above code, you can now replace all instances of C<use
47Moose> with C<use MyApp::Mooseish>. Similarly, C<no Moose> is now
48replaced with C<no MyApp::Mooseish>.
49
d5447d26 50The C<with_meta> parameter specifies a list of functions that should
554b7648 51be wrapped before exporting. The wrapper simply ensures that the
52importing package name is the first argument to the function, so we
53can do C<S<my $caller = shift;>>.
6fa0a13f 54
554b7648 55See the L<Moose::Exporter> docs for more details on its API.
6fa0a13f 56
554b7648 57=head1 USING MyApp::Mooseish
6fa0a13f 58
59The purpose of all this code is to provide a Moose-like
60interface. Here's what it would look like in actual use:
61
62 package MyApp::User;
63
64 use MyApp::Mooseish;
65
66 has_table 'User';
67
554b7648 68 has 'username' => ( is => 'ro' );
69 has 'password' => ( is => 'ro' );
6fa0a13f 70
71 sub login { ... }
72
73 no MyApp::Mooseish;
74
75All of the normal Moose sugar (C<has()>, C<with()>, etc) is available
76when you C<use MyApp::Mooseish>.
77
c09fae0b 78=head1 CONCLUSION
79
80Providing sugar functions can make your extension look much more
81Moose-ish. See L<Fey::ORM> for a more extensive example.
82
c79239a2 83=begin testing
84
85
86{
87 package MyApp::User;
88
89 MyApp::Mooseish->import;
90
91 has_table( 'User' );
92
93 has( 'username' => ( is => 'ro' ) );
94 has( 'password' => ( is => 'ro' ) );
95
96 sub login { }
97}
98
99isa_ok( MyApp::User->meta, 'MyApp::Meta::Class' );
100is( MyApp::User->meta->table, 'User',
101 'MyApp::User->meta->table returns User' );
102ok( MyApp::User->can('username'),
103 'MyApp::User has username method' );
104
105=end testing
106
6fa0a13f 107=pod