update other docs about Moose::Exporter to not use strict/warnings
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Recipe4.pod
1
2 =pod
3
4 =head1 NAME
5
6 Moose::Cookbook::Extending::Recipe4 - Acting like Moose.pm and providing sugar Moose-style
7
8 =head1 SYNOPSIS
9
10   package MyApp::Mooseish;
11
12   use Moose ();
13   use Moose::Exporter;
14
15   Moose::Exporter->setup_import_methods(
16       with_caller => ['has_table'],
17       also        => 'Moose',
18   );
19
20   sub init_meta {
21       shift;
22       return Moose->init_meta( @_, metaclass => 'MyApp::Meta::Class' );
23   }
24
25   sub has_table {
26       my $caller = shift;
27       $caller->meta->table(shift);
28   }
29
30   package MyApp::Meta::Class;
31   use Moose;
32
33   extends 'Moose::Meta::Class';
34
35   has 'table' => ( is => 'rw' );
36
37 =head1 DESCRIPTION
38
39 This recipe expands on the use of L<Moose::Exporter> we saw in
40 L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own
41 object base class, we provide our own metaclass class, and we also
42 export a C<has_table> sugar function.
43
44 Given the above code, you can now replace all instances of C<use
45 Moose> with C<use MyApp::Mooseish>. Similarly, C<no Moose> is now
46 replaced with C<no MyApp::Mooseish>.
47
48 The C<with_caller> parameter specifies a list of functions that should
49 be wrapped before exporting. The wrapper simply ensures that the
50 importing package name is the first argument to the function, so we
51 can do C<S<my $caller = shift;>>.
52
53 See the L<Moose::Exporter> docs for more details on its API.
54
55 =head1 USING MyApp::Mooseish
56
57 The purpose of all this code is to provide a Moose-like
58 interface. Here's what it would look like in actual use:
59
60   package MyApp::User;
61
62   use MyApp::Mooseish;
63
64   has_table 'User';
65
66   has 'username' => ( is => 'ro' );
67   has 'password' => ( is => 'ro' );
68
69   sub login { ... }
70
71   no MyApp::Mooseish;
72
73 All of the normal Moose sugar (C<has()>, C<with()>, etc) is available
74 when you C<use MyApp::Mooseish>.
75
76 =head1 CONCLUSION
77
78 Providing sugar functions can make your extension look much more
79 Moose-ish. See L<Fey::ORM> for a more extensive example.
80
81 =head1 AUTHOR
82
83 Dave Rolsky E<lt>autarch@urth.orgE<gt>
84
85 =head1 COPYRIGHT AND LICENSE
86
87 Copyright 2006-2009 by Infinity Interactive, Inc.
88
89 L<http://www.iinteractive.com>
90
91 This library is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself.
93
94 =begin testing
95
96
97 {
98     package MyApp::User;
99
100     MyApp::Mooseish->import;
101
102     has_table( 'User' );
103
104     has( 'username' => ( is => 'ro' ) );
105     has( 'password' => ( is => 'ro' ) );
106
107     sub login { }
108 }
109
110 isa_ok( MyApp::User->meta, 'MyApp::Meta::Class' );
111 is( MyApp::User->meta->table, 'User',
112     'MyApp::User->meta->table returns User' );
113 ok( MyApp::User->can('username'),
114     'MyApp::User has username method' );
115
116 =end testing
117
118 =pod