summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authordoy <doy@tozt.net>2009-04-10 23:43:21 -0500
committerdoy <doy@tozt.net>2009-04-10 23:43:21 -0500
commit3edcf2a135e0cf919ac635c87a20635b6e9de74f (patch)
tree0382ef24272c0aa78152cea040fd8f72261b6981 /lib
parent1d9006ef273daf2b564ad5f5f5231264c7b5f5a9 (diff)
downloadmoosex-nonmoose-3edcf2a135e0cf919ac635c87a20635b6e9de74f.tar.gz
moosex-nonmoose-3edcf2a135e0cf919ac635c87a20635b6e9de74f.zip
start trying to use a constructor metaclass role to do things more intelligently
Diffstat (limited to 'lib')
-rw-r--r--lib/MooseX/NonMoose.pm27
-rw-r--r--lib/MooseX/NonMoose/Meta/Role/Constructor.pm6
2 files changed, 23 insertions, 10 deletions
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;