Class: Smartdown::Engine
- Inherits:
-
Object
show all
- Defined in:
- lib/smartdown/engine.rb,
lib/smartdown/engine/state.rb,
lib/smartdown/engine/errors.rb,
lib/smartdown/engine/transition.rb,
lib/smartdown/engine/interpolator.rb,
lib/smartdown/engine/node_presenter.rb,
lib/smartdown/engine/conditional_resolver.rb
Defined Under Namespace
Classes: ConditionalResolver, IndeterminateNextNode, Interpolator, NodePresenter, State, Transition, UndefinedValue
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(flow, initial_state = {}) ⇒ Engine
Returns a new instance of Engine.
10
11
12
13
|
# File 'lib/smartdown/engine.rb', line 10
def initialize(flow, initial_state = {})
@flow = flow
@initial_state = initial_state
end
|
Instance Attribute Details
#flow ⇒ Object
Returns the value of attribute flow.
8
9
10
|
# File 'lib/smartdown/engine.rb', line 8
def flow
@flow
end
|
Instance Method Details
#build_start_state ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/smartdown/engine.rb', line 15
def build_start_state
Smartdown::Engine::State.new(
default_predicates.merge(
current_node: flow.name
)
)
end
|
#default_predicates ⇒ Object
23
24
25
|
# File 'lib/smartdown/engine.rb', line 23
def default_predicates
{}.merge(@initial_state)
end
|
#evaluate_node(state) ⇒ Object
53
54
55
56
|
# File 'lib/smartdown/engine.rb', line 53
def evaluate_node(state)
current_node = flow.node(state.get(:current_node))
NodePresenter.new.present(current_node, state)
end
|
#process(unprocessed_responses, test_start_state = nil) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/smartdown/engine.rb', line 27
def process(unprocessed_responses, test_start_state = nil)
state = test_start_state || build_start_state
while !unprocessed_responses.empty? do
current_node = flow.node(state.get(:current_node))
break if current_node.is_outcome_page_node?
if current_node.is_start_page_node?
answers = unprocessed_responses.shift(1)
else
answers = current_node.questions.map do |question|
question.answer_type.new(unprocessed_responses.shift, question)
end
if answers.any?(&:invalid?)
state = state.put(:current_answers, answers)
break
end
end
transition = Transition.new(state, current_node, answers)
state = transition.next_state
end
state
end
|