Just haven't commited this yet, and it has a lot of work in it.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Base.pm
index 4a87a8c..9938528 100644 (file)
@@ -2,6 +2,7 @@
 package MooseX::AttributeHelpers::Base;
 use Moose;
 use Moose::Util::TypeConstraints;
+use MooseX::AttributeHelpers::Meta::Method::Provided;
 
 our $VERSION   = '0.04';
 our $AUTHORITY = 'cpan:STEVAN';
@@ -15,7 +16,6 @@ has 'provides' => (
     default => sub {{}}
 );
 
-
 # these next two are the possible methods
 # you can use in the 'provides' map.
 
@@ -54,14 +54,23 @@ has '+type_constraint' => (required => 1);
 
 ## Methods called prior to instantiation
 
-sub helper_type { () }
+# (overridden by Sugar or plain subclasses)
+sub helper_type {()}
+sub default_options {}
+sub auto_provide {0}
 
 sub process_options_for_provides {
     my ($self, $options) = @_;
 
+    if (my $defaults = $self->default_options) {
+        foreach my $key (keys %$defaults) {
+            $options->{$key} = $defaults->{$key} 
+                unless exists $options->{$key};
+        }
+    }
+
     if (my $type = $self->helper_type) {
-        (exists $options->{isa})
-            || confess "You must define a type with the $type metaclass";
+        $options->{isa} = $type unless exists $options->{isa};
 
         my $isa = $options->{isa};
 
@@ -92,6 +101,15 @@ sub check_provides_values {
         (exists $method_constructors->{$key})
             || confess "$key is an unsupported method type";
     }
+
+    my $provides = $self->provides;
+    if (keys %$provides == 0 and $self->auto_provide) {
+        my $attr_name = $self->name;
+
+        foreach my $method (keys %$method_constructors) {
+          $provides->{$method} = "${method}_${attr_name}";
+        }
+    }
 }
 
 after 'install_accessors' => sub {
@@ -159,7 +177,10 @@ MooseX::AttributeHelpers::Base - Base class for attribute helpers
 
 =head1 DESCRIPTION
 
-Documentation to come.
+This class is what you inherit from when you want to make a new
+AttributeHelper.  Unless you are doing something quite fancy, your needs
+should be met by L<MooseX::AttributeHelpers::Sugar>, which has a nice, terse
+syntax and some convenience, but you should still subclass this class.
 
 =head1 ATTRIBUTES
 
@@ -167,10 +188,21 @@ Documentation to come.
 
 =item B<provides>
 
+This is the map of metaclass methods to methods that will be installed in your
+class, e.g. add => 'add_to_number'.
+
 =item B<method_provider>
 
+The name of a class or role to be used as source material for the above map.
+In the above example, the method provider's "add" method would be used to
+construct a method to install into the attribute holder's class.
+
 =item B<method_constructors>
 
+You can optionally supply a hashref of names to subs instead of a class to be
+used as method constructors, but by default this is pulled from
+method_provider.
+
 =back
 
 =head1 EXTENDED ATTRIBUTES
@@ -193,16 +225,27 @@ C<type_constraint> is now required.
 
 =item B<meta>
 
+=item B<auto_provide>
+
+If this method returns a true value, all available method constructors will be
+provided in the format $method_$attribute_name e.g. inc_counter.  This is
+intended to be overridden in subclasses.
+
+=item B<default_options>
+
+Returns a Maybe[Hashref] of attribution specifications to fill in if they are
+not overridden by the implementing attribute.  This is intended to be
+overridden in subclasses.
+
 =item B<helper_type>
 
-=item B<check_provides_values>
+This forces all attributes using this metaclass to be a subtype of
+helper_type.  This is intended to be overridden in subclasses.
 
-=item B<has_default>
+=item B<check_provides_values>
 
 =item B<has_method_provider>
 
-=item B<has_type_constraint>
-
 =item B<install_accessors>
 
 =item B<remove_accessors>