X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FSingleton%2FRole%2FMeta%2FMethod%2FConstructor.pm;fp=lib%2FMooseX%2FSingleton%2FMeta%2FMethod%2FConstructor.pm;h=d45c91bb26f78b4672ce222338f86adf8f33cd1b;hb=8eec3c69ee4aa161601b0255c3b32cd6d9cc6998;hp=01ea6f4be80d4ed5ad1dbaf887c75e8256808490;hpb=b1882d9b6ec47b18a97dadb6e1326305d890c806;p=gitmo%2FMooseX-Singleton.git diff --git a/lib/MooseX/Singleton/Meta/Method/Constructor.pm b/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm similarity index 67% rename from lib/MooseX/Singleton/Meta/Method/Constructor.pm rename to lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm index 01ea6f4..d45c91b 100644 --- a/lib/MooseX/Singleton/Meta/Method/Constructor.pm +++ b/lib/MooseX/Singleton/Role/Meta/Method/Constructor.pm @@ -1,10 +1,8 @@ #!/usr/bin/env perl -package MooseX::Singleton::Meta::Method::Constructor; -use Moose; +package MooseX::Singleton::Role::Meta::Method::Constructor; +use Moose::Role; -extends 'Moose::Meta::Method::Constructor'; - -sub _initialize_body { +override _initialize_body => sub { my $self = shift; # TODO: # the %options should also include a both @@ -15,7 +13,7 @@ sub _initialize_body { # the author, after all, nothing is free) my $source = 'sub {'; $source .= "\n" . 'my $class = shift;'; - + $source .= "\n" . 'my $existing = do { no strict "refs"; no warnings "once"; \${"$class\::singleton"}; };'; $source .= "\n" . 'return ${$existing} if ${$existing};'; @@ -57,17 +55,30 @@ sub _initialize_body { if $e; $self->{'body'} = $code; -} - -# For CMOP 0.82_01+ -sub _expected_method_class { - return 'MooseX::Singleton::Object'; -} +}; + +# Ideally we'd be setting this in the constructor, but the new() methods in +# what the parent classes are not well-factored. +# +# This is all a nasty hack, though. We need to fix Class::MOP::Inlined to +# allow constructor class roles to say "if the parent class has role X, +# inline". +override _expected_method_class => sub { + my $self = shift; -# For older versions of Moose/CMOP -sub _expected_constructor_class { - return 'MooseX::Singleton::Object'; -} + my $super_value = super(); + if ( $super_value eq 'Moose::Object' ) { + for my $parent ( map { Class::MOP::class_of($_) } + $self->associated_metaclass->superclasses ) { + return $parent->name + if $parent->is_anon_class + && grep { $_->name eq 'Moose::Object' } + map { Class::MOP::class_of($_) } $parent->superclasses; + } + } + + return $super_value; +}; no Moose;