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, initial_state = {}) ⇒ Engine
Returns a new instance of Engine.
9
10
11
12
|
# File 'lib/smartdown/engine.rb', line 9
def initialize(flow, initial_state = {})
@flow = flow
@initial_state = initial_state
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
#build_start_state ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/smartdown/engine.rb', line 14
def build_start_state
Smartdown::Engine::State.new(
default_predicates.merge(
current_node: flow.name
)
)
end
|
#default_predicates ⇒ Object
22
23
24
25
26
|
# File 'lib/smartdown/engine.rb', line 22
def default_predicates
{
otherwise: ->(_) { true }
}.merge(@initial_state)
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, test_start_state = nil) ⇒ Object
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 28
def process(responses, test_start_state = nil)
state = test_start_state || build_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.class.to_s.include?("Smartdown::Model::Element::Question")
}.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
|