use B 'svref_2object';
use Clone ();
-our $VERSION = '0.03';
+our $VERSION = '0.04';
-# Self-introspection
+# Self-introspection
-sub meta { Class::MOP::Class->initialize($_[0]) }
+sub meta { Class::MOP::Class->initialize(blessed($_[0]) || $_[0]) }
# Creation
sub initialize {
my $class = shift;
my $package_name = shift;
- (defined $package_name && $package_name)
- || confess "You must pass a package name";
- # make sure the package name is not blessed
- $package_name = blessed($package_name) || $package_name;
+ (defined $package_name && $package_name && !blessed($package_name))
+ || confess "You must pass a package name and it cannot be blessed";
$class->construct_class_instance(':package' => $package_name, @_);
}
shift @class_list; # shift off $self->name
foreach my $class_name (@class_list) {
- next unless $METAS{$class_name};
my $meta = $METAS{$class_name};
($self->isa(blessed($meta)))
|| confess $self->name . "->meta => (" . (blessed($self)) . ")" .
eval $code;
confess "creation of $package_name failed : $@" if $@;
my $meta = $class->initialize($package_name);
+
+ $meta->add_method('meta' => sub {
+ Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
+ });
+
$meta->superclasses(@{$options{superclasses}})
if exists $options{superclasses};
# NOTE:
next if $seen_class{$class};
$seen_class{$class}++;
# fetch the meta-class ...
- my $meta = $self->initialize($class);
+ my $meta = $self->initialize($class);;
push @methods => {
name => $method_name,
class => $class,
(defined $attribute_name && $attribute_name)
|| confess "You must define an attribute name";
return $self->get_attribute_map->{$attribute_name}
- if $self->has_attribute($attribute_name);
+ if $self->has_attribute($attribute_name);
+ return;
}
sub remove_attribute {
(defined $attribute_name && $attribute_name)
|| confess "You must define an attribute name";
my $removed_attribute = $self->get_attribute_map->{$attribute_name};
- delete $self->get_attribute_map->{$attribute_name}
- if defined $removed_attribute;
+ return unless defined $removed_attribute;
+ delete $self->get_attribute_map->{$attribute_name};
$removed_attribute->remove_accessors();
$removed_attribute->detach_from_class();
return $removed_attribute;
delete ${$self->name . '::'}{$name};
}
-# class mixins
-
-sub mixin {
- my ($self, $mixin) = @_;
- $mixin = $self->initialize($mixin) unless blessed($mixin);
-
- my @attributes = map { $mixin->get_attribute($_)->clone() }
- $mixin->get_attribute_list;
- my %methods = map {
- my $method = $mixin->get_method($_);
- if (blessed($method) && $method->isa('Class::MOP::Attribute::Accessor')) {
- ();
- }
- else {
- ($_ => $method)
- }
- } $mixin->get_method_list;
-
- # test the superclass thing detailed in the test
-
- foreach my $attr (@attributes) {
- $self->add_attribute($attr)
- unless $self->has_attribute($attr->name);
- }
-
- foreach my $method_name (keys %methods) {
- $self->alias_method($method_name => $methods{$method_name})
- unless $self->has_method($method_name);
- }
-}
-
1;
__END__
# use this for introspection ...
- package Foo;
- sub meta { Class::MOP::Class->initialize(__PACKAGE__) }
-
- # elsewhere in the code ...
-
# add a method to Foo ...
Foo->meta->add_method('bar' => sub { ... })