Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Deserialize (some) inputs.

  • Loading branch information...
commit 35b8a64a11b702c51e81fe899269dd3de44920b8 1 parent ef6d970
@threedaymonk threedaymonk authored
View
23 lib/sibyl/input.rb
@@ -0,0 +1,23 @@
+module Sibyl
+ class InputHandler
+ class << self
+ def register(slug, handler)
+ handlers[slug] = handler
+ end
+
+ def deserialize(slug, input)
+ handlers[slug].call(input)
+ end
+
+ private
+ def handlers
+ default = lambda { |s| s }
+ @handlers ||= Hash.new { |h, k| h[k] = default }
+ end
+ end
+ end
+end
+
+Dir[File.expand_path("../input/*.rb", __FILE__)].each do |path|
+ require "sibyl/input/#{File.basename(path, ".rb")}"
+end
View
1  lib/sibyl/input/multiple.rb
@@ -0,0 +1 @@
+Sibyl::InputHandler.register "multiple", lambda { |s| s }
View
1  lib/sibyl/input/number.rb
@@ -0,0 +1 @@
+Sibyl::InputHandler.register "number", lambda { |s| s.to_f }
View
3  lib/sibyl/nodes.rb
@@ -1,4 +1,5 @@
require "sibyl/errors"
+require "sibyl/input"
module Sibyl
module Nodes
@@ -72,7 +73,7 @@ def exits
end
def compute(input, context)
- context.input = input
+ context.input = InputHandler.deserialize(type, input)
statements.each do |s|
s.execute(context)
end
View
4 test/graph_test.rb
@@ -35,7 +35,7 @@ def graph(source)
})
assert_equal "How old are you?", g.at(["yes"]).name
- assert_equal "Adult", g.at(["yes", 19]).name
+ assert_equal "Adult", g.at(["yes", "19"]).name
end
it "should raise an exception when inputs exceed steps" do
@@ -46,7 +46,7 @@ def graph(source)
})
assert_raises Sibyl::InvalidInput do
- g.at([1, 2])
+ g.at(["1", "2"])
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.