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;
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() )
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;
}
# 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)
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