From c94b01a949eadbc8864eac775c526ed8ffeeb98b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 29 May 2009 00:05:47 -0500 Subject: use better logic in the constructor rather than replacing it manually --- lib/MooseX/ABC/Role/Object.pm | 12 +++++++----- lib/MooseX/ABC/Trait/Class.pm | 18 +++--------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/MooseX/ABC/Role/Object.pm b/lib/MooseX/ABC/Role/Object.pm index 083c0f5..778a81a 100644 --- a/lib/MooseX/ABC/Role/Object.pm +++ b/lib/MooseX/ABC/Role/Object.pm @@ -1,12 +1,14 @@ package MooseX::ABC::Role::Object; use Moose::Role; -sub new { +around new => sub { + my $orig = shift; my $class = shift; - Class::MOP::class_of($class)->throw_error( - "$class is abstract, it cannot be instantiated" - ); -} + my $meta = Class::MOP::class_of($class); + $meta->throw_error("$class is abstract, it cannot be instantiated") + if $meta->has_required_methods; + $class->$orig(@_); +}; no Moose::Role; 1; diff --git a/lib/MooseX/ABC/Trait/Class.pm b/lib/MooseX/ABC/Trait/Class.pm index 0b51a75..6f4bf6c 100644 --- a/lib/MooseX/ABC/Trait/Class.pm +++ b/lib/MooseX/ABC/Trait/Class.pm @@ -31,18 +31,6 @@ after _superclasses_updated => sub { } } } - - return if $self->has_required_methods; - - # at this point, the current class didn't have any required methods to - # start with, and all of the required methods from superclasses have - # been satisfied, so restore the constructor - - my $constructor = $self->find_method_by_name('new'); - if ($constructor->original_package_name eq 'MooseX::ABC::Role::Object') { - my $moose_constructor = Class::MOP::class_of('Moose::Object')->get_method('new'); - $self->add_method(new => $moose_constructor->clone); - } }; around _immutable_options => sub { @@ -50,11 +38,11 @@ around _immutable_options => sub { my $self = shift; my @options = $self->$orig(@_); my $constructor = $self->find_method_by_name('new'); - if ($constructor->original_package_name eq 'Moose::Object') { - push @options, replace_constructor => 1; + if ($self->has_required_methods) { + push @options, inline_constructor => 0; } elsif ($constructor->original_package_name eq 'MooseX::ABC::Role::Object') { - push @options, inline_constructor => 0; + push @options, replace_constructor => 1; } return @options; }; -- cgit v1.2.3