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 L<its documentation|MooseX::AttributeHelpers::Base> for
84 =item B<default_options> I<HashRef>
86 =item B<helper_type> I<String>
88 =item B<auto_provide> I<Bool>
90 =item B<method_provider> I<ClassName>
92 =item B<method_constructors> I<HashRef>
98 For ease of use of the generated metaclasses, if you pass in a "shortcut"
99 parameter to define_attribute_helper, a class at
100 Moose::Meta::Attribute::Custom::$shortcut will be generated for you, which
101 allows clients of your class to specify their metaclass by this shortcut
102 (without the long prefix).
106 All complex software has bugs lurking in it, and this module is no
107 exception. If you find a bug please either email me, or add the bug
112 Paul Driver E<lt> frodwith at cpan.org E<gt>
114 =head1 COPYRIGHT AND LICENSE
116 Copyright 2007, 2008 by Infinity Interactive, Inc.
118 L<http://www.iinteractive.com>
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.