Bump all versions to 0.16 (from *cough* 0.14!)
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Base.pm
index 95f8b72..7ee5af6 100644 (file)
@@ -3,7 +3,8 @@ package MooseX::AttributeHelpers::Base;
 use Moose;
 use Moose::Util::TypeConstraints;
 
-our $VERSION   = '0.02';
+our $VERSION   = '0.16';
+$VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
 extends 'Moose::Meta::Attribute';
@@ -15,12 +16,17 @@ has 'provides' => (
     default => sub {{}}
 );
 
+has 'curries' => (
+    is      => 'ro',
+    isa     => 'HashRef',
+    default => sub {{}}
+);
 
-# these next two are the possible methods 
+# these next two are the possible methods
 # you can use in the 'provides' map.
 
-# provide a Class or Role which we can 
-# collect the method providers from 
+# provide a Class or Role which we can
+# collect the method providers from
 has 'method_provider' => (
     is        => 'ro',
     isa       => 'ClassName',
@@ -29,7 +35,7 @@ has 'method_provider' => (
 
 # or you can provide a HASH ref of anon subs
 # yourself. This will also collect and store
-# the methods from a method_provider as well 
+# the methods from a method_provider as well
 has 'method_constructors' => (
     is      => 'ro',
     isa     => 'HashRef',
@@ -40,16 +46,16 @@ has 'method_constructors' => (
         # or grab them from the role/class
         my $method_provider = $self->method_provider->meta;
         return +{
-            map { 
+            map {
                 $_ => $method_provider->get_method($_)
             } $method_provider->get_method_list
-        };            
+        };
     },
 );
 
-# extend the parents stuff to make sure 
+# 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
@@ -58,15 +64,15 @@ sub helper_type { () }
 
 sub process_options_for_provides {
     my ($self, $options) = @_;
-    
+
     if (my $type = $self->helper_type) {
         (exists $options->{isa})
-            || confess "You must define a type with the $type metaclass";  
+            || confess "You must define a type with the $type metaclass";
 
-        my $isa = $options->{isa};       
+        my $isa = $options->{isa};
 
         unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
-            $isa = find_type_constraint($isa);        
+            $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
         }
 
         ($isa->is_a_type_of($type))
@@ -76,73 +82,148 @@ sub process_options_for_provides {
 
 before '_process_options' => sub {
     my ($self, $name, $options) = @_;
-    if (exists $options->{provides} || 
-        exists $options->{isa}      && $options->{isa} =~ /^.*?\[.*?\]$/) {
-        $self->process_options_for_provides($options);
-    }
+    $self->process_options_for_provides($options, $name);
 };
 
 ## 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;
-    
+
     my $method_constructors = $self->method_constructors;
-    
+
     foreach my $key (keys %{$self->provides}) {
         (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;
+
+    my @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 {
     my $attr  = shift;
     my $class = $attr->associated_class;
-    
+
     # grab the reader and writer methods
-    # as well, this will be useful for 
+    # as well, this will be useful for
     # our method provider constructors
-    my ($attr_reader, $attr_writer);
-    if (my $reader = $attr->get_read_method) {    
-        $attr_reader = $class->get_method($reader);
-    }
-    else {
-        $attr_reader = sub { $attr->get_value(@_) };
-    }
-    
-    if (my $writer = $attr->get_write_method) {    
-        $attr_writer = $class->get_method($writer);
-    }
-    else {
-        $attr_writer = sub { $attr->set_value(@_) };
-    }        
+    my $attr_reader = $attr->get_read_method_ref;
+    my $attr_writer = $attr->get_write_method_ref;
+
 
     # before we install them, lets
     # make sure they are valid
-    $attr->check_provides_values;    
+    $attr->check_provides_values;
 
     my $method_constructors = $attr->method_constructors;
-    
+
+    my $class_name = $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);
+        }
+    }
+
     foreach my $key (keys %{$attr->provides}) {
-        
-        my $method_name = $attr->provides->{$key};       
-        my $method_body = $method_constructors->{$key}->(
-            $attr,
-            $attr_reader,
-            $attr_writer,            
-        );
-        
+
+        my $method_name = $attr->provides->{$key};
+
         if ($class->has_method($method_name)) {
             confess "The method ($method_name) already exists in class (" . $class->name . ")";
         }
-        
-        $class->add_method($method_name => 
-            MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
-                $method_body,
-            )
+
+        my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
+            $method_constructors->{$key}->(
+                $attr,
+                $attr_reader,
+                $attr_writer,
+            ),
+            package_name => $class_name,
+            name => $method_name,
         );
+        
+        $attr->associate_method($method);
+        $class->add_method($method_name => $method);
+    }
+};
+
+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);
+        $class->remove_method($method_name)
+            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');
     }
 };
 
@@ -158,7 +239,7 @@ __END__
 =head1 NAME
 
 MooseX::AttributeHelpers::Base - Base class for attribute helpers
-  
+
 =head1 DESCRIPTION
 
 Documentation to come.
@@ -169,6 +250,8 @@ Documentation to come.
 
 =item B<provides>
 
+=item B<curries>
+
 =item B<method_provider>
 
 =item B<method_constructors>
@@ -179,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>
 
@@ -193,6 +276,8 @@ C<type_constraint> is now required.
 
 =over 4
 
+=item B<meta>
+
 =item B<helper_type>
 
 =item B<check_provides_values>
@@ -205,13 +290,15 @@ C<type_constraint> is now required.
 
 =item B<install_accessors>
 
+=item B<remove_accessors>
+
 =item B<process_options_for_provides>
 
 =back
 
 =head1 BUGS
 
-All complex software has bugs lurking in it, and this module is no 
+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.
 
@@ -221,7 +308,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2007 by Infinity Interactive, Inc.
+Copyright 2007-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>