Commit | Line | Data |
6fa0a13f |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
c8d5f1e1 |
6 | Moose::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( |
d5447d26 |
16 | with_meta => ['has_table'], |
17 | also => 'Moose', |
554b7648 |
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 { |
d5447d26 |
26 | my $meta = shift; |
27 | $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 |
39 | This recipe expands on the use of L<Moose::Exporter> we saw in |
9a8b19be |
40 | L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own |
41 | object base class, we provide our own metaclass class, and we also |
c09fae0b |
42 | export a C<has_table> sugar function. |
6fa0a13f |
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 | |
d5447d26 |
48 | The C<with_meta> parameter specifies a list of functions that should |
554b7648 |
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;>>. |
6fa0a13f |
52 | |
554b7648 |
53 | See the L<Moose::Exporter> docs for more details on its API. |
6fa0a13f |
54 | |
554b7648 |
55 | =head1 USING MyApp::Mooseish |
6fa0a13f |
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 | |
554b7648 |
66 | has 'username' => ( is => 'ro' ); |
67 | has 'password' => ( is => 'ro' ); |
6fa0a13f |
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 | |
c09fae0b |
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 | |
6fa0a13f |
81 | =head1 AUTHOR |
82 | |
83 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
84 | |
85 | =head1 COPYRIGHT AND LICENSE |
86 | |
2840a3b2 |
87 | Copyright 2006-2009 by Infinity Interactive, Inc. |
6fa0a13f |
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 | |
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 | |
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 | |
6fa0a13f |
118 | =pod |