use Sub::Name 'subname';
use B 'svref_2object';
-our $VERSION = '0.18';
+our $VERSION = '0.19';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Class::MOP::Module';
$meta = bless {
# inherited from Class::MOP::Package
'$:package' => $package_name,
- '%:namespace' => \%{$package_name . '::'},
+
+ # NOTE:
+ # since the following attributes will
+ # actually be loaded from the symbol
+ # table, and actually bypass the instance
+ # entirely, we can just leave these things
+ # listed here for reference, because they
+ # should not actually have a value associated
+ # with the slot.
+ '%:namespace' => \undef,
# inherited from Class::MOP::Module
- '$:version' => (exists ${$package_name . '::'}{'VERSION'} ? ${$package_name . '::VERSION'} : undef),
- '$:authority' => (exists ${$package_name . '::'}{'AUTHORITY'} ? ${$package_name . '::AUTHORITY'} : undef),
- # defined here ...
- '%:attributes' => {},
+ '$:version' => \undef,
+ '$:authority' => \undef,
+ # defined in Class::MOP::Class
+
+ '%:methods' => {},
+ '%:attributes' => {},
'$:attribute_metaclass' => $options{':attribute_metaclass'} || 'Class::MOP::Attribute',
'$:method_metaclass' => $options{':method_metaclass'} || 'Class::MOP::Method',
'$:instance_metaclass' => $options{':instance_metaclass'} || 'Class::MOP::Instance',
sub create_anon_class {
my ($class, %options) = @_;
my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
- return $class->create($package_name, '0.00', %options);
+ return $class->create($package_name, %options);
}
# NOTE:
# creating classes with MOP ...
sub create {
- my ($class, $package_name, $package_version, %options) = @_;
+ my $class = shift;
+ my $package_name = shift;
+
(defined $package_name && $package_name)
|| confess "You must pass a package name";
+
+ (scalar @_ % 2 == 0)
+ || confess "You much pass all parameters as name => value pairs " .
+ "(I found an uneven number of params in \@_)";
+
+ my (%options) = @_;
+
my $code = "package $package_name;";
- $code .= "\$$package_name\:\:VERSION = '$package_version';"
- if defined $package_version;
+ $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
+ if exists $options{version};
+ $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
+ if exists $options{authority};
+
eval $code;
confess "creation of $package_name failed : $@" if $@;
+
my $meta = $class->initialize($package_name);
$meta->add_method('meta' => sub {
sub method_metaclass { $_[0]->{'$:method_metaclass'} }
sub instance_metaclass { $_[0]->{'$:instance_metaclass'} }
+sub get_method_map {
+ my $self = shift;
+ my $map = $self->{'%:methods'};
+
+ foreach my $symbol (grep { $self->has_package_symbol('&' . $_) } $self->list_all_package_symbols) {
+ next if exists $map->{$symbol} &&
+ $map->{$symbol}->body == $self->get_package_symbol('&' . $symbol);
+
+ $map->{$symbol} = $self->method_metaclass->wrap(
+ $self->get_package_symbol('&' . $symbol)
+ );
+ }
+
+ return $map;
+}
+
# Instance Construction & Cloning
sub new_object {
(defined $method_name && $method_name)
|| confess "You must define a method name";
# use reftype here to allow for blessed subs ...
- ('CODE' eq (reftype($method) || ''))
- || confess "Your code block must be a CODE reference";
- my $full_method_name = ($self->name . '::' . $method_name);
-
- # FIXME:
- # dont bless subs, its bad mkay
- $method = $self->method_metaclass->wrap($method) unless blessed($method);
- $self->add_package_symbol("&${method_name}" => subname $full_method_name => $method);
+ my $body;
+
+ if (blessed($method)) {
+
+ $body = $method->body;
+
+ ('CODE' eq (reftype($body) || ''))
+ || confess "Your code block must be a CODE reference";
+
+ $self->get_method_map->{$method_name} = $method;
+ }
+ else {
+
+ $body = $method;
+
+ ('CODE' eq (reftype($body) || ''))
+ || confess "Your code block must be a CODE reference";
+
+ $self->get_method_map->{$method_name} = $self->method_metaclass->wrap($body);
+
+ }
+
+ my $full_method_name = ($self->name . '::' . $method_name);
+ $self->add_package_symbol("&${method_name}" => subname $full_method_name => $body);
}
{
my ($self, $method_name, $method) = @_;
(defined $method_name && $method_name)
|| confess "You must define a method name";
- # use reftype here to allow for blessed subs ...
- ('CODE' eq (reftype($method) || ''))
- || confess "Your code block must be a CODE reference";
- # FIXME:
- # dont bless subs, its bad mkay
- $method = $self->method_metaclass->wrap($method) unless blessed($method);
-
- $self->add_package_symbol("&${method_name}" => $method);
-}
+ my $body;
-sub find_method_by_name {
- my ($self, $method_name) = @_;
- return $self->name->can($method_name);
+ if (blessed($method)) {
+
+ $body = $method->body;
+
+ ('CODE' eq (reftype($body) || ''))
+ || confess "Your code block must be a CODE reference";
+
+ $self->get_method_map->{$method_name} = $method;
+ }
+ else {
+
+ $body = $method;
+
+ ('CODE' eq (reftype($body) || ''))
+ || confess "Your code block must be a CODE reference";
+
+ $self->get_method_map->{$method_name} = $self->method_metaclass->wrap($body);
+
+ }
+
+ $self->add_package_symbol("&${method_name}" => $body);
}
sub has_method {
(defined $method_name && $method_name)
|| confess "You must define a method name";
- return 0 if !$self->has_package_symbol("&${method_name}");
- my $method = $self->get_package_symbol("&${method_name}");
- return 0 if (svref_2object($method)->GV->STASH->NAME || '') ne $self->name &&
- (svref_2object($method)->GV->NAME || '') ne '__ANON__';
-
- # FIXME:
- # dont bless subs, its bad mkay
- $self->method_metaclass->wrap($method) unless blessed($method);
+ my $method_map = $self->get_method_map;
+
+ return 0 unless exists $self->get_method_map->{$method_name};
+
+ my $method = $method_map->{$method_name};
+ return 0 if ($method->package_name || '') ne $self->name &&
+ ($method->name || '') ne '__ANON__';
return 1;
}
my ($self, $method_name) = @_;
(defined $method_name && $method_name)
|| confess "You must define a method name";
-
+
return unless $self->has_method($method_name);
- return $self->get_package_symbol("&${method_name}");
+ return $self->get_method_map->{$method_name};
}
sub remove_method {
$self->remove_package_symbol("&${method_name}")
if defined $removed_method;
+ delete $self->get_method_map->{$method_name}
+ if exists $self->get_method_map->{$method_name};
+
return $removed_method;
}
sub get_method_list {
my $self = shift;
- grep { $self->has_method($_) } $self->list_all_package_symbols;
+ return grep { $self->has_method($_) } keys %{$self->get_method_map};
+}
+
+sub find_method_by_name {
+ my ($self, $method_name) = @_;
+ # FIXME
+ return $self->name->can($method_name);
}
sub compute_all_applicable_methods {
# or use this to actually create classes ...
- Class::MOP::Class->create('Bar' => '0.01' => (
+ Class::MOP::Class->create('Bar' => (
+ version => '0.01',
superclasses => [ 'Foo' ],
attributes => [
Class::MOP:::Attribute->new('$bar'),
=over 4
-=item B<create ($package_name, ?$package_version,
+=item B<create ($package_name,
+ version =E<gt> ?$version,
+ authority =E<gt> ?$authority,
superclasses =E<gt> ?@superclasses,
methods =E<gt> ?%methods,
attributes =E<gt> ?%attributes)>
This returns a B<Class::MOP::Class> object, bringing the specified
-C<$package_name> into existence and adding any of the
-C<$package_version>, C<@superclasses>, C<%methods> and C<%attributes>
-to it.
+C<$package_name> into existence and adding any of the C<$version>,
+C<$authority>, C<@superclasses>, C<%methods> and C<%attributes> to
+it.
=item B<create_anon_class (superclasses =E<gt> ?@superclasses,
methods =E<gt> ?%methods,
=over 4
+=item B<get_method_map>
+
=item B<method_metaclass>
=item B<add_method ($method_name, $method)>