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