6 Moose::Cookbook::Extending::Recipe4 - Acting like Moose.pm and providing sugar Moose-style
10 package MyApp::Mooseish;
15 Moose::Exporter->setup_import_methods(
16 with_caller => ['has_table'],
22 return Moose->init_meta( @_, metaclass => 'MyApp::Meta::Class' );
27 $caller->meta->table(shift);
30 package MyApp::Meta::Class;
33 extends 'Moose::Meta::Class';
35 has 'table' => ( is => 'rw' );
39 This recipe expands on the use of L<Moose::Exporter> we saw in
40 L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own
41 object base class, we provide our own metaclass class, and we also
42 export a C<has_table> sugar function.
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>.
48 The C<with_caller> parameter specifies a list of functions that should
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;>>.
53 See the L<Moose::Exporter> docs for more details on its API.
55 =head1 USING MyApp::Mooseish
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:
66 has 'username' => ( is => 'ro' );
67 has 'password' => ( is => 'ro' );
73 All of the normal Moose sugar (C<has()>, C<with()>, etc) is available
74 when you C<use MyApp::Mooseish>.
78 Providing sugar functions can make your extension look much more
79 Moose-ish. See L<Fey::ORM> for a more extensive example.
83 Dave Rolsky E<lt>autarch@urth.orgE<gt>
85 =head1 COPYRIGHT AND LICENSE
87 Copyright 2006-2009 by Infinity Interactive, Inc.
89 L<http://www.iinteractive.com>
91 This library is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself.
100 MyApp::Mooseish->import;
104 has( 'username' => ( is => 'ro' ) );
105 has( 'password' => ( is => 'ro' ) );
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' );