From 3edcf2a135e0cf919ac635c87a20635b6e9de74f Mon Sep 17 00:00:00 2001 From: doy Date: Fri, 10 Apr 2009 23:43:21 -0500 Subject: start trying to use a constructor metaclass role to do things more intelligently --- lib/MooseX/NonMoose.pm | 27 +++++++++++++++++---------- lib/MooseX/NonMoose/Meta/Role/Constructor.pm | 6 ++++++ 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 lib/MooseX/NonMoose/Meta/Role/Constructor.pm (limited to 'lib') diff --git a/lib/MooseX/NonMoose.pm b/lib/MooseX/NonMoose.pm index c7c542d..bdd2ad4 100644 --- a/lib/MooseX/NonMoose.pm +++ b/lib/MooseX/NonMoose.pm @@ -16,18 +16,23 @@ sub extends { Moose::extends($caller, @superclasses); my $caller_meta = Class::MOP::Class->initialize($caller); - # XXX: this doesn't need to be installed for every class, just for the - # classes directly below ones with custom constructors... how can we do - # this? + # we need to get the non-moose constructor from the superclass + # of the class where this method actually exists + my $super_new = $caller_meta->find_next_method_by_name('new'); + + if ($super_new->associated_metaclass->can('constructor_class')) { + my $constructor_class_meta = Class::MOP::Class->initialize( + $super_new->associated_metaclass->constructor_class + ); + + return if $constructor_class_meta->can('does_role') + && $constructor_class_meta->does_role('MooseX::NonMoose::Meta::Role::Constructor'); + } + $caller_meta->add_method(new => sub { my $class = shift; my $meta = Class::MOP::Class->initialize($class); - # we need to get the non-moose constructor from the superclass - # of the class where this method actually exists - my $super_new = $caller_meta->find_next_method_by_name('new'); - # but we need to call it as a method on the class we're actually - # trying to instantiate my $self = $super_new->execute($class, @_); my $params = $class->BUILDARGS(@_); @@ -45,8 +50,10 @@ sub init_meta { my %options = @_; Moose->init_meta(%options); Moose::Util::MetaRole::apply_metaclass_roles( - for_class => $options{for_class}, - metaclass_roles => ['MooseX::NonMoose::Meta::Role::Class'], + for_class => $options{for_class}, + metaclass_roles => ['MooseX::NonMoose::Meta::Role::Class'], + constructor_metaclass_roles => + ['MooseX::NonMoose::Meta::Role::Constructor'], ); return $options{for_class}->meta; } diff --git a/lib/MooseX/NonMoose/Meta/Role/Constructor.pm b/lib/MooseX/NonMoose/Meta/Role/Constructor.pm new file mode 100644 index 0000000..81b59ab --- /dev/null +++ b/lib/MooseX/NonMoose/Meta/Role/Constructor.pm @@ -0,0 +1,6 @@ +package MooseX::NonMoose::Meta::Role::Constructor; +use Moose::Role; + +no Moose::Role; + +1; -- cgit v1.2.3-54-g00ecf