Use Moose::_get_caller (unseemly, but maybe more flexible).
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute.pm
index cdf664d..9eb4d88 100644 (file)
@@ -3,13 +3,14 @@ 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 Moose::Meta::Class;
 use Sub::Name;
 
 
@@ -23,8 +24,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 +56,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";
-        }
-
-        $code .= <<'EOF';
+        push @parents, 'Moose::Object'
+            unless grep { $_->isa('Moose::Object') } @parents;
 
-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;
     }
@@ -93,7 +87,7 @@ EOF
 # This is basically copied from Moose.pm
 sub unimport ## no critic RequireFinalReturn
 {
-    my $caller = caller();
+    my $caller = Moose::_get_caller(@_);
 
     no strict 'refs'; ## no critic ProhibitNoStrict
     foreach my $name (@EXPORT)
@@ -159,6 +153,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