Permalink
Browse files

Add in some examples for Action and Container, borrowed from the test…

… suite
  • Loading branch information...
1 parent c99a95c commit 471b18a63f505081d70366d3c526bd5426fae2eb @Whiteknight committed Mar 17, 2011
Showing with 83 additions and 0 deletions.
  1. +44 −0 libraries/action.md
  2. +39 −0 libraries/container.md
View
@@ -93,6 +93,50 @@ be passed every time the Action is invoked.
### NQP-rx
+Use an Action.Sub to create a replace operation which we can use on multiple
+strings:
+
+ my $action := Rosella::build(Rosella::Action::Sub,
+ sub($item) {
+ $item.replace("This", "That");
+ }, []
+ );
+ my $data_a := "This is a test";
+ $action.execute($data_a);
+ pir::say($data_a); # "That is a test"
+ my $data_b := "I Like This";
+ $action.execute($data_b);
+ pir::say($data_b); # "I Like That"
+
+A similar example, showing the use of Arguments:
+
+ my $action := Rosella::build(Rosella::Action::Sub,
+ sub($item, $a, $b) {
+ $item.replace($a, $b);
+ }, [
+ Rosella::build(Rosella::Action::Argument::Instance, "a", :position(0)),
+ Rosella::build(Rosella::Action::Argument::Instance, "b", :position(1))
+ ]
+ );
+ my $data := "This is a test";
+ $action.execute($data); # "This is b test"
+
+And another similar example, showing the use of overrides during execution
+
+ my $data := "This is a test";
+ my $action := Rosella::build(Rosella::Action::Sub,
+ sub($item, $a, $b) {
+ $item.replace($a, $b);
+ }, [
+ Rosella::build(Rosella::Action::Argument::Instance, "a", :position(0)),
+ Rosella::build(Rosella::Action::Argument::Instance, "b", :position(1))
+ ]
+ );
+ $action.execute($data, [
+ Rosella::build(Rosella::Action::Argument::Instance, "i", :position(0)),
+ ]);
+ pir::say($data); # "Thbs bs a test"
+
## Users
* The Rosella Container library uses Action
View
@@ -134,4 +134,43 @@ Container object to resolve an argument.
### NQP-rx
+Register the type "String" to return a string. This string will have an
+initial value, and will then be modified:
+
+ my $c := Rosella::build(Rosella::Container);
+ $c.register_type("String",
+ :meth_inits([
+ Rosella::build(Rosella::Action::Sub,
+ sub ($obj) {
+ pir::set__vPS($obj, "FooBarBaz");
+ }, []
+ ),
+ Rosella::build(Rosella::Action::Method,
+ "replace", [
+ Rosella::build(Rosella::Action::Argument::Instance, "B", :position(0)),
+ Rosella::build(Rosella::Action::Argument::Instance, "C", :position(1))
+ ]
+ )
+ ]
+ ));
+ my $bar := $c.resolve("String"); # "FooCarCaz"
+
+Register an entry which returns a unique ID integer every time it is resolved:
+
+ my $c := Rosella::build(Rosella::Container);
+ my $seed := pir::box__PI(5);
+ $c.register_instance("UniqueID",
+ $seed
+ :meth_inits([
+ Rosella::build(Rosella::Action::Sub,
+ sub ($obj) {
+ pir::inc__vP($obj);
+ }, []
+ ),
+ ]
+ ));
+ my $a := $c.resolve("UniqueID"); # 6
+ my $b := $c.resolve("UniqueID"); # 7
+ my $c := $c.resolve("UniqueID"); # 8
+
## Users

0 comments on commit 471b18a

Please sign in to comment.