Skip to content

Commit

Permalink
Separate out .create and .construct behaviors in Prototype::Item, so …
Browse files Browse the repository at this point in the history
…we can call one or the other independently
  • Loading branch information
Whiteknight committed Feb 18, 2011
1 parent ac9d2a2 commit 4e41253
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
7 changes: 5 additions & 2 deletions prototype/Item.nqp
Expand Up @@ -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?) {
Expand Down
15 changes: 14 additions & 1 deletion prototype/Manager.nqp
Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions t/prototype/Item.t
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4e41253

Please sign in to comment.