refactor code to allow for class attributes in roles
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute / Role / Meta / Class.pm
index 7ec220f..e51d0a6 100644 (file)
@@ -3,57 +3,48 @@ package MooseX::ClassAttribute::Role::Meta::Class;
 use strict;
 use warnings;
 
-use MooseX::AttributeHelpers;
 use MooseX::ClassAttribute::Role::Meta::Attribute;
 use Scalar::Util qw( blessed );
 
+use namespace::autoclean;
 use Moose::Role;
 
-has class_attribute_map => (
-    metaclass => 'Collection::Hash',
-    is        => 'ro',
-    isa       => 'HashRef[Moose::Meta::Attribute]',
-    provides  => {
-        set    => '_add_class_attribute',
-        exists => 'has_class_attribute',
-        get    => 'get_class_attribute',
-        delete => '_remove_class_attribute',
-        keys   => 'get_class_attribute_list',
-    },
-    default => sub { {} },
-    reader  => 'get_class_attribute_map',
-);
+with 'MooseX::ClassAttribute::Role::Meta::Mixin::HasClassAttributes';
 
 has _class_attribute_values => (
-    metaclass => 'Collection::Hash',
-    is        => 'ro',
-    isa       => 'HashRef',
-    provides  => {
-        get    => 'get_class_attribute_value',
-        set    => 'set_class_attribute_value',
-        exists => 'has_class_attribute_value',
-        delete => 'clear_class_attribute_value',
+    traits  => ['Hash'],
+    is      => 'ro',
+    isa     => 'HashRef',
+    handles => {
+        'get_class_attribute_value'   => 'get',
+        'set_class_attribute_value'   => 'set',
+        'has_class_attribute_value'   => 'exists',
+        'clear_class_attribute_value' => 'delete',
     },
-    lazy    => 1,
-    default => sub { $_[0]->_class_attribute_values_hashref() },
+    lazy     => 1,
+    default  => sub { $_[0]->_class_attribute_values_hashref() },
+    init_arg => undef,
 );
 
-sub add_class_attribute {
+around add_class_attribute => sub {
+    my $orig = shift;
     my $self = shift;
-
-    my $attr
-        = blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
+    my $attr = (
+        blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
         ? $_[0]
-        : $self->_process_class_attribute(@_);
+        : $self->_process_class_attribute(@_)
+    );
 
-    my $name = $attr->name();
+    $self->$orig($attr);
 
-    $self->remove_class_attribute($name)
-        if $self->has_class_attribute($name);
+    return $attr;
+};
 
-    $attr->attach_to_class($self);
+sub _post_add_class_attribute {
+    my $self = shift;
+    my $attr = shift;
 
-    $self->_add_class_attribute( $name => $attr );
+    my $name = $attr->name();
 
     my $e = do {
         local $@;
@@ -65,8 +56,11 @@ sub add_class_attribute {
         $self->remove_attribute($name);
         die $e;
     }
+}
 
-    return $attr;
+sub _attach_class_attribute {
+    my ($self, $attribute) = @_;
+    $attribute->attach_to_class($self);
 }
 
 # It'd be nice if I didn't have to replicate this for class
@@ -234,8 +228,6 @@ sub inline_weaken_class_slot_value {
         . $self->inline_class_slot_access($name) . ')';
 }
 
-no Moose::Role;
-
 1;
 
 __END__