From: Dave Rolsky Date: Sun, 5 Apr 2009 22:22:09 +0000 (-0500) Subject: changes to work with Moose 0.73_01+ X-Git-Tag: 0.15~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0cd38a85e3f9a77914e846ad84e290b22b501b5a;p=gitmo%2FMooseX-Singleton.git changes to work with Moose 0.73_01+ modernized handling of constructor class, and modernized the constructor generation itself --- diff --git a/Makefile.PL b/Makefile.PL index 1e2ff81..61c200d 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -5,7 +5,7 @@ use inc::Module::Install; name 'MooseX-Singleton'; all_from 'lib/MooseX/Singleton.pm'; -requires 'Moose' => '0.65'; +requires 'Moose' => '0.73_01'; build_requires 'Test::More'; build_requires 'Test::Exception'; diff --git a/lib/MooseX/Singleton.pm b/lib/MooseX/Singleton.pm index 8261d0e..2915697 100644 --- a/lib/MooseX/Singleton.pm +++ b/lib/MooseX/Singleton.pm @@ -1,6 +1,6 @@ package MooseX::Singleton; -use Moose (); +use Moose 0.73_01 (); use Moose::Exporter; use MooseX::Singleton::Object; use MooseX::Singleton::Meta::Class; diff --git a/lib/MooseX/Singleton/Meta/Class.pm b/lib/MooseX/Singleton/Meta/Class.pm index 3a0d5d6..529095b 100644 --- a/lib/MooseX/Singleton/Meta/Class.pm +++ b/lib/MooseX/Singleton/Meta/Class.pm @@ -2,6 +2,7 @@ package MooseX::Singleton::Meta::Class; use Moose; use MooseX::Singleton::Meta::Instance; +use MooseX::Singleton::Meta::Method::Constructor; extends 'Moose::Meta::Class'; @@ -9,12 +10,15 @@ sub initialize { my $class = shift; my $pkg = shift; - $class->SUPER::initialize( + my $self = $class->SUPER::initialize( $pkg, instance_metaclass => 'MooseX::Singleton::Meta::Instance', + constructor_class => 'MooseX::Singleton::Meta::Method::Constructor', @_, ); -}; + + return $self; +} sub existing_singleton { my ($class) = @_; @@ -30,7 +34,7 @@ sub existing_singleton { return; } -override construct_instance => sub { +override _construct_instance => sub { my ($class) = @_; # create exactly one instance @@ -42,20 +46,8 @@ override construct_instance => sub { return ${"$pkg\::singleton"} = super; }; -# Need to remove make_immutable before we define it below no Moose; -use MooseX::Singleton::Meta::Method::Constructor; - -sub make_immutable { - my $self = shift; - $self->SUPER::make_immutable - ( - constructor_class => 'MooseX::Singleton::Meta::Method::Constructor', - @_, - ); -} - 1; __END__ diff --git a/lib/MooseX/Singleton/Meta/Method/Constructor.pm b/lib/MooseX/Singleton/Meta/Method/Constructor.pm index eb8aaab..b24db58 100644 --- a/lib/MooseX/Singleton/Meta/Method/Constructor.pm +++ b/lib/MooseX/Singleton/Meta/Method/Constructor.pm @@ -4,7 +4,7 @@ use Moose; extends 'Moose::Meta::Method::Constructor'; -sub initialize_body { +sub _initialize_body { my $self = shift; # TODO: # the %options should also include a both @@ -22,13 +22,9 @@ sub initialize_body { $source .= "\n" . 'return $class->Moose::Object::new(@_)'; $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; - $source .= "\n" . 'my $params = ' . $self->_generate_BUILDARGS('$class', '@_'); - - $source .= ";\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class'); - - $source .= ";\n" . (join ";\n" => map { - $self->_generate_slot_initializer($_) - } 0 .. (@{$self->attributes} - 1)); + $source .= $self->_generate_params('$params', '$class'); + $source .= $self->_generate_instance('$instance', '$class'); + $source .= $self->_generate_slot_initializers; $source .= ";\n" . $self->_generate_triggers(); $source .= ";\n" . $self->_generate_BUILDALL(); @@ -37,38 +33,26 @@ sub initialize_body { $source .= ";\n" . '}'; warn $source if $self->options->{debug}; - my $code; - { - my $meta = $self; + my $attrs = $self->_attributes; + + my @type_constraints = map { + $_->can('type_constraint') ? $_->type_constraint : undef + } @$attrs; - # NOTE: - # create the nessecary lexicals - # to be picked up in the eval - my $attrs = $self->attributes; + my @type_constraint_bodies = map { + defined $_ ? $_->_compiled_type_constraint : undef; + } @type_constraints; - # We need to check if the attribute ->can('type_constraint') - # since we may be trying to immutabilize a Moose meta class, - # which in turn has attributes which are Class::MOP::Attribute - # objects, rather than Moose::Meta::Attribute. And - # Class::MOP::Attribute attributes have no type constraints. - # However we need to make sure we leave an undef value there - # because the inlined code is using the index of the attributes - # to determine where to find the type constraint - - my @type_constraints = map { - $_->can('type_constraint') ? $_->type_constraint : undef - } @$attrs; - - my @type_constraint_bodies = map { - defined $_ ? $_->_compiled_type_constraint : undef; - } @type_constraints; + my $code = $self->_compile_code( + code => $source, + environment => { + '$meta' => \$self, + '$attrs' => \$attrs, + '@type_constraints' => \@type_constraints, + '@type_constraint_bodies' => \@type_constraint_bodies, + }, + ) or $self->throw_error("Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source ); - $code = eval $source; - $self->throw_error( - "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@", - error => $@, data => $source ) - if $@; - } $self->{'body'} = $code; }