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/predicate_evaluator.rb,
lib/smartdown/engine/conditional_resolver.rb
Defined Under Namespace
Classes: ConditionalResolver, IndeterminateNextNode, Interpolator, NodePresenter, PredicateEvaluator, State, Transition, UndefinedValue
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(flow) ⇒ Engine
Returns a new instance of Engine.
9
10
11
|
# File 'lib/smartdown/engine.rb', line 9
def initialize(flow)
@flow = flow
end
|
Instance Attribute Details
#flow ⇒ Object
Returns the value of attribute flow.
7
8
9
|
# File 'lib/smartdown/engine.rb', line 7
def flow
@flow
end
|
Instance Method Details
#default_predicates ⇒ Object
21
22
23
24
25
|
# File 'lib/smartdown/engine.rb', line 21
def default_predicates
{
otherwise: ->(_) { true }
}
end
|
#default_start_state ⇒ Object
13
14
15
16
17
18
19
|
# File 'lib/smartdown/engine.rb', line 13
def default_start_state
Smartdown::Engine::State.new(
default_predicates.merge(
current_node: flow.name
)
)
end
|
#evaluate_node(state) ⇒ Object
49
50
51
52
|
# File 'lib/smartdown/engine.rb', line 49
def evaluate_node(state)
current_node = flow.node(state.get(:current_node))
NodePresenter.new.present(current_node, state)
end
|
#process(responses, 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
|
# File 'lib/smartdown/engine.rb', line 27
def process(responses, start_state = nil)
state = start_state || default_start_state
unprocessed_responses = responses
while !unprocessed_responses.empty? do
nb_questions = 0
current_node = flow.node(state.get(:current_node))
nb_questions += current_node.elements.select{|element|
element.is_a? Smartdown::Model::Element::MultipleChoice
}.count
nb_relevant_inputs = [nb_questions, 1].max
input_array = unprocessed_responses.take(nb_relevant_inputs)
unprocessed_responses = unprocessed_responses.drop(nb_relevant_inputs)
transition = Transition.new(state, current_node, input_array)
state = transition.next_state
end
state
end
|