more tweaks, I think I want to make this into a role
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
index 3bce3b0..83ede3b 100644 (file)
@@ -1,63 +1,37 @@
 
 package MooseX::AttributeHelpers::Counter;
 use Moose;
-use Moose::Util::TypeConstraints;
 
 our $VERSION   = '0.01';
 our $AUTHORITY = 'cpan:STEVAN';
 
-extends 'Moose::Meta::Attribute';
-
-my %METHOD_CONSTRUCTORS = (
-    inc => sub {
-        my $attr = shift;
-        return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
-    },
-    dec => sub {
-        my $attr = shift;
-        return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };        
-    },
-);
-
-has 'provides' => (
-    is       => 'ro',
-    isa      => subtype('HashRef' => where { 
-        (exists $METHOD_CONSTRUCTORS{$_} || return) for keys %{$_}; 1;
-    }),
-    required => 1,
-);
-
-has '+$!default'       => (required => 1);
-has '+type_constraint' => (required => 1);
-
-before '_process_options' => sub {
-    my ($self, %options) = @_;
-    
-    if (exists $options{provides}) {
-        (exists $options{isa})
-            || confess "You must define a type with the Counter metaclass";  
-             
-        (find_type_constraint($options{isa})->is_subtype_of('Num'))
-            || confess "The type constraint for a Counter must be a subtype of Num";
+extends 'MooseX::AttributeHelpers::Base';
+
+has '+method_constructors' => (
+    default => sub {
+        return +{
+            inc => sub {
+                my $attr = shift;
+                return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
+            },
+            dec => sub {
+                my $attr = shift;
+                return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };        
+            },
+        }
     }
-};
+);
 
-after 'install_accessors' => sub {
-    my $attr  = shift;
-    my $class = $attr->associated_class;
+sub _process_options_for_provides {
+    my ($self, $options) = @_;
+    (exists $options->{isa})
+        || confess "You must define a type with the Counter metaclass";  
+     
+    (find_type_constraint($options->{isa})->is_subtype_of('Num'))
+        || confess "The type constraint for a Counter must be a subtype of Num";
+}
     
-    foreach my $key (keys %{$attr->provides}) {
-        (exists $METHOD_CONSTRUCTORS{$key})
-            || confess "Unsupported method type ($key)";
-        $class->add_method(
-            $attr->provides->{$key}, 
-            $METHOD_CONSTRUCTORS{$key}->($attr)
-        );
-    }
-};
-
 no Moose;
-no Moose::Util::TypeConstraints;
 
 # register the alias ...
 package Moose::Meta::Attribute::Custom::Counter;