Tweaks based on Stevan's changes to make greater use of the
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute.pm
index cdf664d..8a0c4a2 100644 (file)
@@ -3,11 +3,11 @@ package MooseX::ClassAttribute;
 use strict;
 use warnings;
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 our $AUTHORITY = 'cpan:DROLSKY';
 
 our @EXPORT = 'class_has'; ## no critic ProhibitAutomaticExportation
-use Exporter qw( import );
+use base 'Exporter';
 
 use B qw( svref_2object );
 use Sub::Name;
@@ -23,8 +23,7 @@ sub class_has ## no critic RequireArgUnpacking
 
     my $container_pkg = _make_container_class( $caller, @parents );
 
-    my $has = $container_pkg->can('has');
-    $has->(@_);
+    $container_pkg->meta()->_process_attribute(@_);
 
     my $container_meta = $container_pkg->meta();
     for my $meth ( grep { $_ ne 'instance' } $container_meta->get_method_list() )
@@ -56,28 +55,22 @@ sub class_has ## no critic RequireArgUnpacking
 
         my @parents = map { container_class($_) || () } @_;
 
-        my $container_pkg = 'MooseX::ClassAttribute::Container::' . $caller;
-
-        my $code = "package $container_pkg;\n";
-        $code .= "use Moose;\n\n";
-
-        if (@parents)
-        {
-            $code .= "extends qw( @parents );\n";
-        }
+        push @parents, 'Moose::Object'
+            unless grep { $_->isa('Moose::Object') } @parents;
 
-        $code .= <<'EOF';
-
-my $Self;
-sub instance
-{
-    return $Self ||= shift->new(@_);
-}
-EOF
+        my $container_pkg = 'MooseX::ClassAttribute::Container::' . $caller;
 
+        my $instance_meth = sub {
+            no strict 'refs'; ## no critic ProhibitNoStrict
+            return ${ $container_pkg . '::Self' } ||= shift->new(@_);
+        };
 
-        eval $code; ## no critic ProhibitStringyEval
-        die $@ if $@;
+        my $class =
+            Moose::Meta::Class->create
+                ( $container_pkg =>
+                  superclasses => \@parents,
+                  methods      => { instance => $instance_meth },
+                );
 
         return $Name{$caller} = $container_pkg;
     }
@@ -159,6 +152,10 @@ You can use any feature of Moose's attribute declarations, including
 overriding a parent's attributes, delegation (C<handles>), and
 attribute metaclasses, and it should just work.
 
+The accessors methods for class attribute may be called on the class
+directly, or on objects of that class. Passing a class attribute to
+the constructor will not set it.
+
 =head1 FUNCTIONS
 
 This class exports one function when you use it, C<class_has()>. This