2 package MooseX::AttributeHelpers::Sugar;
4 use Exporter qw(import);
5 our @EXPORT = qw(define_attribute_helper);
7 sub define_attribute_helper (%) {
10 my $meta = $class->meta;
12 $meta->add_method('helper_type', sub {$info{helper_type}});
13 $meta->add_method('default_options', sub {$info{default_options}});
14 $meta->add_method('auto_provide', sub {$info{auto_provide} || 0});
16 if(my $provider = $info{method_provider}) {
17 eval "require $provider";
18 confess "Error loading method provider" if $@;
19 $meta->add_attribute('+method_provider', default => $provider);
22 if (my $cons = $info{method_constructors}) {
23 $meta->add_attribute('+method_constructors', default => $cons)
26 if (my $s = $info{shortcut}) {
27 $meta->create("Moose::Meta::Attribute::Custom::$s",
28 methods => {register_implementation => sub { $class }},
41 MooseX::AttributeHelpers::Sugar - Convenience for defining AttributeHelper
46 package MooseX::AttributeHelpers::Counter;
48 use MooseX::AttributeHelpers::Sugar;
50 extends 'MooseX::AttributeHelpers::Base';
52 define_attribute_helper (
59 method_provider => 'MooseX::AttributeHelpers::MethodProvider::Counter',
61 shortcut => 'Counter',
65 no MooseX::AttributeHelpers::Sugar;
71 This is just sugar to let you declaratively subclass
72 L<MooseX::AttributeHelpers::Base>. You still need to explicitly subclass, but
73 most of the boilerplate is taken care of for you by the sugar. One function is
78 =item B<define_attribute_helper>
80 The following parameters are accepted, and are used to override methods in
81 the base class (see its documentation for details).
83 =item B<default_options> I<HashRef>
85 =item B<helper_type> I<String>
87 =item B<auto_provide> I<Bool>
89 =item B<method_provider> I<ClassName>
91 =item B<method_constructors> I<HashRef>
97 For ease of use of the generated metaclasses, if you pass in a "shortcut"
98 parameter to define_attribute_helper, a class at
99 Moose::Meta::Attribute::Custom::$shortcut will be generated for you, which
100 allows clients of your class to specify their metaclass by this shortcut
101 (without the long prefix).
105 All complex software has bugs lurking in it, and this module is no
106 exception. If you find a bug please either email me, or add the bug
111 Paul Driver E<lt> frodwith at cpan.org E<gt>
113 =head1 COPYRIGHT AND LICENSE
115 Copyright 2007, 2008 by Infinity Interactive, Inc.
117 L<http://www.iinteractive.com>
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.