Skip to content
Browse files

Start walking the graph based on inputs.

  • Loading branch information...
1 parent 06a6dc4 commit 723977073b2eab2c882dff8bea1a4405332603cc @threedaymonk threedaymonk committed Mar 22, 2012
Showing with 28 additions and 0 deletions.
  1. +10 −0 lib/sibyl/graph.rb
  2. +18 −0 test/graph_test.rb
View
10 lib/sibyl/graph.rb
@@ -29,6 +29,16 @@ def validate!
raise InvalidGraph.new(e)
end
+ def at(inputs)
+ step = @first_step
+ context = OpenStruct.new
+ inputs.each do |input|
+ result = step.compute(input, context)
+ step = @steps_by_name[result]
+ end
+ step
+ end
+
def l10n_keys
@steps.inject([]) { |keys, step|
keys + step.l10n_keys
View
18 test/graph_test.rb
@@ -19,4 +19,22 @@ def graph(source)
assert_equal expected, g.metadata
end
+
+ it "should walk the nodes according to inputs" do
+ g = graph(%{
+ step multiple "Yes or no?"
+ option yes -> "How old are you?"
+ option no -> "Whatever"
+ step number "How old are you?"
+ go ->
+ if { input > 18 } -> "Adult"
+ otherwise -> "Child"
+ outcome "Whatever"
+ outcome "Adult"
+ outcome "Child"
+ })
+
+ assert_equal "How old are you?", g.at(["yes"]).name
+ assert_equal "Adult", g.at(["yes", 19]).name
+ end
end

0 comments on commit 7239770

Please sign in to comment.
Something went wrong with that request. Please try again.