1 package Moose::Cookbook::Extending::Recipe4;
3 # ABSTRACT: Acting like Moose.pm and providing sugar Moose-style
12 package MyApp::Mooseish;
16 Moose::Exporter->setup_import_methods(
17 with_meta => ['has_table'],
19 class => ['MyApp::Meta::Class::Trait::HasTable'],
28 package MyApp::Meta::Class::Trait::HasTable;
38 This recipe expands on the use of L<Moose::Exporter> we saw in
39 L<Moose::Cookbook::Extending::Recipe1> and the class metaclass trait we saw in
40 L<Moose::Cookbook::Meta::Recipe5>. In this example we provide our own
41 metaclass trait, and we also export a C<has_table> sugar function.
43 The C<with_meta> parameter specifies a list of functions that should
44 be wrapped before exporting. The wrapper simply ensures that the
45 importing package's appropriate metaclass object is the first argument
46 to the function, so we can do C<S<my $meta = shift;>>.
48 See the L<Moose::Exporter> docs for more details on its API.
50 =head1 USING MyApp::Mooseish
52 The purpose of all this code is to provide a Moose-like
53 interface. Here's what it would look like in actual use:
57 use namespace::autoclean;
64 has 'username' => ( is => 'ro' );
65 has 'password' => ( is => 'ro' );
71 Providing sugar functions can make your extension look much more
72 Moose-ish. See L<Fey::ORM> for a more extensive example.
80 MyApp::Mooseish->import;
84 has( 'username' => ( is => 'ro' ) );
85 has( 'password' => ( is => 'ro' ) );
90 isa_ok( MyApp::User->meta, 'MyApp::Meta::Class' );
91 is( MyApp::User->meta->table, 'User',
92 'MyApp::User->meta->table returns User' );
93 ok( MyApp::User->can('username'),
94 'MyApp::User has username method' );