Bump all versions to 0.16 (from *cough* 0.14!)
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Base.pm
index bc72738..7ee5af6 100644 (file)
@@ -3,7 +3,8 @@ package MooseX::AttributeHelpers::Base;
 use Moose;
 use Moose::Util::TypeConstraints;
 
-our $VERSION   = '0.04';
+our $VERSION   = '0.16';
+$VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
 extends 'Moose::Meta::Attribute';
@@ -21,7 +22,6 @@ has 'curries' => (
     default => sub {{}}
 );
 
-
 # these next two are the possible methods
 # you can use in the 'provides' map.
 
@@ -55,7 +55,7 @@ has 'method_constructors' => (
 
 # extend the parents stuff to make sure
 # certain bits are now required ...
-has '+$!default'       => (required => 1);
+has '+default'         => (required => 1);
 has '+type_constraint' => (required => 1);
 
 ## Methods called prior to instantiation
@@ -87,7 +87,7 @@ before '_process_options' => sub {
 
 ## methods called after instantiation
 
-# this confirms that provides has
+# this confirms that provides (and curries) has
 # all valid possibilities in it
 sub check_provides_values {
     my $self = shift;
@@ -98,15 +98,33 @@ sub check_provides_values {
         (exists $method_constructors->{$key})
             || confess "$key is an unsupported method type";
     }
+
+    foreach my $key (keys %{$self->curries}) {
+        (exists $method_constructors->{$key})
+            || confess "$key is an unsupported method type";
+    }
 }
 
 sub _curry {
     my $self = shift;
     my $code = shift;
 
-    #warn "_curry: "; use DDS; warn Dump($self);
     my @args = @_;
-    return sub { my $self = shift; $code->($self, @args, @_) };
+    return sub {
+        my $self = shift;
+        $code->($self, @args, @_)
+    };
+}
+
+sub _curry_sub {
+    my $self = shift;
+    my $body = shift;
+    my $code = shift;
+
+    return sub {
+        my $self = shift;
+        $code->($self, $body, @_)
+    };
 }
 
 after 'install_accessors' => sub {
@@ -123,34 +141,44 @@ after 'install_accessors' => sub {
     # before we install them, lets
     # make sure they are valid
     $attr->check_provides_values;
-#    $attr->check_curries_values;
 
     my $method_constructors = $attr->method_constructors;
 
     my $class_name = $class->name;
 
-    foreach my $key (keys %{$attr->curries}) {
-
-        my ($curried_name, @curried_args) = @{ $attr->curries->{$key} };
-
-        if ($class->has_method($curried_name)) {
-            confess "The method ($curried_name) already exists in class (" . $class->name . ")";
+    while (my ($constructor, $constructed) = each %{$attr->curries}) {
+        my $method_code;
+        while (my ($curried_name, $curried_arg) = each(%$constructed)) {
+            if ($class->has_method($curried_name)) {
+                confess
+                    "The method ($curried_name) already ".
+                    "exists in class (" . $class->name . ")";
+            }
+            my $body = $method_constructors->{$constructor}->(
+                       $attr,
+                       $attr_reader,
+                       $attr_writer,
+            );
+
+            if (ref $curried_arg eq 'ARRAY') {
+                $method_code = $attr->_curry($body, @$curried_arg);
+            }
+            elsif (ref $curried_arg eq 'CODE') {
+                $method_code = $attr->_curry_sub($body, $curried_arg);
+            }
+            else {
+                confess "curries parameter must be ref type HASH or CODE";
+            }
+
+            my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
+                $method_code,
+                package_name => $class_name,
+                name => $curried_name,
+            );
+                
+            $attr->associate_method($method);
+            $class->add_method($curried_name => $method);
         }
-
-        my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
-            $attr->_curry($method_constructors->{$key}->(
-                $attr,
-                $attr_reader,
-                $attr_writer,
-            ), @curried_args),
-            package_name => $class_name,
-            name => $curried_name,
-        );
-        
-#use DDS; warn Dump($method);
-
-        $attr->associate_method($method);
-        $class->add_method($curried_name => $method);
     }
 
     foreach my $key (keys %{$attr->provides}) {
@@ -179,6 +207,8 @@ after 'install_accessors' => sub {
 after 'remove_accessors' => sub {
     my $attr  = shift;
     my $class = $attr->associated_class;
+
+    # provides accessors
     foreach my $key (keys %{$attr->provides}) {
         my $method_name = $attr->provides->{$key};
         my $method = $class->get_method($method_name);
@@ -186,6 +216,15 @@ after 'remove_accessors' => sub {
             if blessed($method) &&
                $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
     }
+
+    # curries accessors
+    foreach my $key (keys %{$attr->curries}) {
+        my $method_name = $attr->curries->{$key};
+        my $method = $class->get_method($method_name);
+        $class->remove_method($method_name)
+            if blessed($method) &&
+               $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
+    }
 };
 
 no Moose;
@@ -211,6 +250,8 @@ Documentation to come.
 
 =item B<provides>
 
+=item B<curries>
+
 =item B<method_provider>
 
 =item B<method_constructors>
@@ -221,9 +262,9 @@ Documentation to come.
 
 =over 4
 
-=item B<$!default>
+=item B<default>
 
-C<$!default> is now required.
+C<default> is now required.
 
 =item B<type_constraint>