1 package Moose::Cookbook::Extending::Mooseish_MooseSugar;
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::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
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;>>.
49 See the L<Moose::Exporter> docs for more details on its API.
51 =head1 USING MyApp::Mooseish
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:
58 use namespace::autoclean;
65 has 'username' => ( is => 'ro' );
66 has 'password' => ( is => 'ro' );
72 Providing sugar functions can make your extension look much more
73 Moose-ish. See L<Fey::ORM> for a more extensive example.
82 MyApp::Mooseish->import;
86 has( 'username' => ( is => 'ro' ) );
87 has( 'password' => ( is => 'ro' ) );
92 can_ok( MyApp::User->meta, 'table' );
93 is( MyApp::User->meta->table, 'User',
94 'MyApp::User->meta->table returns User' );
95 ok( MyApp::User->can('username'),
96 'MyApp::User has username method' );