There is no extending recipe3 any more
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Mooseish_MooseSugar.pod
1 package Moose::Cookbook::Extending::Mooseish_MooseSugar;
2
3 # ABSTRACT: Acting like Moose.pm and providing sugar Moose-style
4
5 __END__
6
7
8 =pod
9
10 =head1 SYNOPSIS
11
12   package MyApp::Mooseish;
13
14   use Moose::Exporter;
15
16   Moose::Exporter->setup_import_methods(
17       with_meta       => ['has_table'],
18       class_metaroles => {
19           class => ['MyApp::Meta::Class::Trait::HasTable'],
20       },
21   );
22
23   sub has_table {
24       my $meta = shift;
25       $meta->table(shift);
26   }
27
28   package MyApp::Meta::Class::Trait::HasTable;
29   use Moose::Role;
30
31   has table => (
32       is  => 'rw',
33       isa => 'Str',
34   );
35
36 =head1 DESCRIPTION
37
38 This recipe expands on the use of L<Moose::Exporter> we saw in
39 L<Moose::Cookbook::Extending::ExtensionOverview> and the class metaclass trait
40 we saw in L<Moose::Cookbook::Meta::Table_MetaclassTrait>. In this example we
41 provide our own metaclass trait, and we also export a C<has_table> sugar
42 function.
43
44 The C<with_meta> parameter specifies a list of functions that should
45 be wrapped before exporting. The wrapper simply ensures that the
46 importing package's appropriate metaclass object is the first argument
47 to the function, so we can do C<S<my $meta = shift;>>.
48
49 See the L<Moose::Exporter> docs for more details on its API.
50
51 =head1 USING MyApp::Mooseish
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 namespace::autoclean;
59
60   use Moose;
61   use MyApp::Mooseish;
62
63   has_table 'User';
64
65   has 'username' => ( is => 'ro' );
66   has 'password' => ( is => 'ro' );
67
68   sub login { ... }
69
70 =head1 CONCLUSION
71
72 Providing sugar functions can make your extension look much more
73 Moose-ish. See L<Fey::ORM> for a more extensive example.
74
75 =begin testing
76
77
78 {
79     package MyApp::User;
80
81     MyApp::Mooseish->import;
82
83     has_table( 'User' );
84
85     has( 'username' => ( is => 'ro' ) );
86     has( 'password' => ( is => 'ro' ) );
87
88     sub login { }
89 }
90
91 isa_ok( MyApp::User->meta, 'MyApp::Meta::Class' );
92 is( MyApp::User->meta->table, 'User',
93     'MyApp::User->meta->table returns User' );
94 ok( MyApp::User->can('username'),
95     'MyApp::User has username method' );
96
97 =end testing
98
99 =pod