From 4e4125323a5e43c7bf94485794109d75ff94c69a Mon Sep 17 00:00:00 2001 From: Whiteknight Date: Fri, 18 Feb 2011 11:19:36 -0500 Subject: [PATCH] Separate out .create and .construct behaviors in Prototype::Item, so we can call one or the other independently --- prototype/Item.nqp | 7 +++++-- prototype/Manager.nqp | 15 ++++++++++++++- t/prototype/Item.t | 12 +++++++----- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/prototype/Item.nqp b/prototype/Item.nqp index 079a2c8f..c09ee902 100644 --- a/prototype/Item.nqp +++ b/prototype/Item.nqp @@ -9,12 +9,15 @@ class Rosella::Prototype::Item { } } - method create(@pos, %named) { + method create() { my $item := pir::clone($!prototype); + return $item; + } + + method construct($item, @pos?, %named?) { if pir::defined(&!constructor) { Rosella::call_parrot_method($item, &!constructor, @pos, %named); } - return $item; } method prototype($proto?) { diff --git a/prototype/Manager.nqp b/prototype/Manager.nqp index cb045c87..d093d90e 100644 --- a/prototype/Manager.nqp +++ b/prototype/Manager.nqp @@ -12,8 +12,21 @@ class Rosella::Prototype::Manager { %!library{$name} := $entry } + # Get a fresh instance method instance($name, *@pos, *%named) { - return %!library{$name}.create(@pos, %named); + my $entry := %!library{$name}; + my $obj := $entry.create(); + $entry.construct(@pos, %named); + return $obj; + } + + # Get a fresh instance, and run it through a specified constructor + # instead of any already-registered constructors. + method instance_constructor($name, &constructor, *@pos, *%named) { + my $entry := %!library{$name}; + my $obj := $entry.create(); + Rosella::call_parrot_method($obj, &constructor, @pos, %named); + return $obj; } method get_prototype($name) { diff --git a/t/prototype/Item.t b/t/prototype/Item.t index e533e36f..fbeec6f5 100644 --- a/t/prototype/Item.t +++ b/t/prototype/Item.t @@ -14,25 +14,27 @@ class PrototypeItemTest is Rosella::Testcase { method test_create() { my $proto := "This is a string"; my $item := Rosella::build(Rosella::Prototype::Item, $proto); - my $other := $item.create([], {}); + my $other := $item.create(); Assert::equal($proto, $other); Assert::not_same($proto, $other); } - method test_create_constructor() { + method test_construct() { my $proto := "This is a string"; my &const := sub($self) { $self.replace("This", "That"); }; my $item := Rosella::build(Rosella::Prototype::Item, $proto, &const); - my $other := $item.create([], {}); + my $other := $item.create(); + $item.construct($other); Assert::equal($other, "That is a string"); Assert::not_equal($other, $proto); } - method test_create_constructor_args() { + method test_construct_args() { my $proto := "This is a string"; my &const := sub($self, $a, $b) { $self.replace($a, $b); }; my $item := Rosella::build(Rosella::Prototype::Item, $proto, &const); - my $other := $item.create(["string", "test"], {}); + my $other := $item.create(); + $item.construct($other, ["string", "test"]); Assert::equal($other, "This is a test"); Assert::not_equal($other, $proto); }