Commit | Line | Data |
6fa0a13f |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Extending::Recipe2 - Acting like Moose.pm and providing sugar Moose-style |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | package MyApp::Mooseish; |
11 | |
12 | use strict; |
13 | use warnings; |
14 | |
6fa0a13f |
15 | use Moose (); |
554b7648 |
16 | use Moose::Exporter; |
6fa0a13f |
17 | |
aedcb7d9 |
18 | Moose::Exporter->setup_import_methods( |
554b7648 |
19 | with_caller => ['has_table'], |
20 | also => 'Moose', |
21 | ); |
6fa0a13f |
22 | |
554b7648 |
23 | sub init_meta { |
24 | shift; |
25 | Moose->init_meta( @_, metaclass => 'MyApp::Meta::Class' ); |
6fa0a13f |
26 | } |
27 | |
28 | sub has_table { |
554b7648 |
29 | my $caller = shift; |
6fa0a13f |
30 | $caller->meta()->table(shift); |
31 | } |
32 | |
33 | =head1 DESCRIPTION |
34 | |
554b7648 |
35 | This recipe expands on the use of L<Moose::Exporter> we saw in |
9a8b19be |
36 | L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own |
37 | object base class, we provide our own metaclass class, and we also |
38 | export a sugar subroutine C<has_table()>. |
6fa0a13f |
39 | |
40 | Given the above code, you can now replace all instances of C<use |
41 | Moose> with C<use MyApp::Mooseish>. Similarly, C<no Moose> is now |
42 | replaced with C<no MyApp::Mooseish>. |
43 | |
554b7648 |
44 | The C<with_caller> parameter specifies a list of functions that should |
45 | be wrapped before exporting. The wrapper simply ensures that the |
46 | importing package name is the first argument to the function, so we |
47 | can do C<S<my $caller = shift;>>. |
6fa0a13f |
48 | |
554b7648 |
49 | See the L<Moose::Exporter> docs for more details on its API. |
6fa0a13f |
50 | |
554b7648 |
51 | =head1 USING MyApp::Mooseish |
6fa0a13f |
52 | |
53 | The purpose of all this code is to provide a Moose-like |
54 | interface. Here's what it would look like in actual use: |
55 | |
56 | package MyApp::User; |
57 | |
58 | use MyApp::Mooseish; |
59 | |
60 | has_table 'User'; |
61 | |
554b7648 |
62 | has 'username' => ( is => 'ro' ); |
63 | has 'password' => ( is => 'ro' ); |
6fa0a13f |
64 | |
65 | sub login { ... } |
66 | |
67 | no MyApp::Mooseish; |
68 | |
69 | All of the normal Moose sugar (C<has()>, C<with()>, etc) is available |
70 | when you C<use MyApp::Mooseish>. |
71 | |
6fa0a13f |
72 | =head1 AUTHOR |
73 | |
74 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
75 | |
76 | =head1 COPYRIGHT AND LICENSE |
77 | |
78 | Copyright 2006-2008 by Infinity Interactive, Inc. |
79 | |
80 | L<http://www.iinteractive.com> |
81 | |
82 | This library is free software; you can redistribute it and/or modify |
83 | it under the same terms as Perl itself. |
84 | |
85 | =pod |