Forgot to add a couple of files inthe last commit.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Sugar.pm
diff --git a/lib/MooseX/AttributeHelpers/Sugar.pm b/lib/MooseX/AttributeHelpers/Sugar.pm
new file mode 100644 (file)
index 0000000..2809f17
--- /dev/null
@@ -0,0 +1,122 @@
+
+package MooseX::AttributeHelpers::Sugar;
+use Carp qw(confess);
+use Exporter qw(import);
+our @EXPORT = qw(define_attribute_helper);
+
+sub define_attribute_helper (%) {
+    my %info = @_;
+    my $class = caller();
+    my $meta = $class->meta;
+
+    $meta->add_method('helper_type',     sub {$info{helper_type}});
+    $meta->add_method('default_options', sub {$info{default_options}});
+    $meta->add_method('auto_provide',    sub {$info{auto_provide} || 0});
+
+    if(my $provider = $info{method_provider}) {
+        eval "require $provider";
+        confess "Error loading method provider" if $@;
+        $meta->add_attribute('+method_provider', default => $provider);
+    }
+
+    if (my $cons = $info{method_constructors}) {
+        $meta->add_attribute('+method_constructors', default => $cons)
+    }
+    
+    if (my $s = $info{shortcut}) {
+        $meta->create("Moose::Meta::Attribute::Custom::$s",
+            methods => {register_implementation => sub { $class }},
+        );
+    }
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+MooseX::AttributeHelpers::Sugar - Convenience for defining AttributeHelper
+metaclasses.
+
+=head1 SYNOPSIS
+
+    package MooseX::AttributeHelpers::Counter;
+    use Moose;
+    use MooseX::AttributeHelpers::Sugar;
+
+    extends 'MooseX::AttributeHelpers::Base';
+
+    define_attribute_helper (
+        default_options  => {
+            is      => 'ro', 
+            default => 0,
+        },
+
+        helper_type      => 'Num',
+        method_provider  => 'MooseX::AttributeHelpers::MethodProvider::Counter',
+        auto_provide     => 1,
+        shortcut         => 'Counter',
+    );
+
+    no Moose;
+    no MooseX::AttributeHelpers::Sugar;
+
+    1;
+
+=head1 DESCRIPTION
+
+This is just sugar to let you declaratively subclass
+L<MooseX::AttributeHelpers::Base>.  You still need to explicitly subclass, but
+most of the boilerplate is taken care of for you by the sugar.  One function is
+exported.
+
+=over 4
+
+=item B<define_attribute_helper>
+
+The following parameters are accepted, and are used to override methods in 
+the base class (see its documentation for details).
+
+=item B<default_options> I<HashRef>
+
+=item B<helper_type> I<String>
+
+=item B<auto_provide> I<Bool>
+
+=item B<method_provider> I<ClassName>
+
+=item B<method_constructors> I<HashRef>
+
+=back
+
+=head SHORTCUT
+
+For ease of use of the generated metaclasses, if you pass in a "shortcut"
+parameter to define_attribute_helper, a class at
+Moose::Meta::Attribute::Custom::$shortcut will be generated for you, which
+allows clients of your class to specify their metaclass by this shortcut
+(without the long prefix).
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no 
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Paul Driver E<lt> frodwith at cpan.org E<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007, 2008 by Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut