-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathstate.rb
47 lines (39 loc) · 1.04 KB
/
state.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'ostruct'
module SmartAnswer
class State < OpenStruct
def initialize(start_node)
super(current_node: start_node, path: [], responses: [], response: nil, error: nil)
end
def method_missing(method_name, *args)
if respond_to_missing?(method_name)
super
else
raise NoMethodError.new("undefined method '#{method_name}' for #{self.class}")
end
end
def respond_to_missing?(method_name, include_private = false)
method_name =~ /=$/ || super
end
def transition_to(new_node, input, &blk)
dup.tap { |new_state|
new_state.path << self.current_node
new_state.current_node = new_node
new_state.responses << input
yield new_state if block_given?
new_state.freeze
}
end
def to_hash
@table
end
def save_input_as(name)
__send__ "#{name}=", responses.last
end
private
def initialize_copy(orig)
super
self.responses = orig.responses.dup
self.path = orig.path.dup
end
end
end