Skip to content
This repository has been archived by the owner on Apr 4, 2018. It is now read-only.

Commit

Permalink
Deserialize (some) inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
threedaymonk committed Mar 23, 2012
1 parent ef6d970 commit 35b8a64
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
23 changes: 23 additions & 0 deletions lib/sibyl/input.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/sibyl/input/multiple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sibyl::InputHandler.register "multiple", lambda { |s| s }
1 change: 1 addition & 0 deletions lib/sibyl/input/number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sibyl::InputHandler.register "number", lambda { |s| s.to_f }
3 changes: 2 additions & 1 deletion lib/sibyl/nodes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "sibyl/errors"
require "sibyl/input"

module Sibyl
module Nodes
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/graph_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,7 +46,7 @@ def graph(source)
})

assert_raises Sibyl::InvalidInput do
g.at([1, 2])
g.at(["1", "2"])
end
end
end

0 comments on commit 35b8a64

Please sign in to comment.