Class: Inquirex::Transition
- Inherits:
-
Object
- Object
- Inquirex::Transition
- Defined in:
- lib/inquirex/transition.rb
Overview
A single edge from one step to another, optionally guarded by a rule. Transitions are evaluated in order; the first whose rule is true determines the next step. Can carry a ‘requires_server` flag for steps that need a server round-trip (e.g. LLM steps).
Instance Attribute Summary collapse
-
#requires_server ⇒ Boolean
readonly
whether this transition needs a server round-trip.
-
#rule ⇒ Rules::Base?
readonly
condition; nil means unconditional (always applies).
-
#target ⇒ Symbol
readonly
id of the step to go to when this transition applies.
Class Method Summary collapse
-
.from_h(hash) ⇒ Transition
Deserializes from a plain Hash.
Instance Method Summary collapse
-
#applies?(answers) ⇒ Boolean
Whether this transition should be taken given current answers.
-
#condition_label ⇒ String
Human-readable label for the condition.
-
#initialize(target:, rule: nil, requires_server: false) ⇒ Transition
constructor
A new instance of Transition.
-
#to_h ⇒ Hash
Serializes to a plain Hash for JSON output.
Constructor Details
#initialize(target:, rule: nil, requires_server: false) ⇒ Transition
Returns a new instance of Transition.
17 18 19 20 21 22 |
# File 'lib/inquirex/transition.rb', line 17 def initialize(target:, rule: nil, requires_server: false) @target = target.to_sym @rule = rule @requires_server = requires_server freeze end |
Instance Attribute Details
#requires_server ⇒ Boolean (readonly)
whether this transition needs a server round-trip
11 12 13 |
# File 'lib/inquirex/transition.rb', line 11 def requires_server @requires_server end |
#rule ⇒ Rules::Base? (readonly)
condition; nil means unconditional (always applies)
11 12 13 |
# File 'lib/inquirex/transition.rb', line 11 def rule @rule end |
#target ⇒ Symbol (readonly)
id of the step to go to when this transition applies
11 12 13 |
# File 'lib/inquirex/transition.rb', line 11 def target @target end |
Class Method Details
.from_h(hash) ⇒ Transition
Deserializes from a plain Hash.
55 56 57 58 59 60 61 |
# File 'lib/inquirex/transition.rb', line 55 def self.from_h(hash) target = hash["to"] || hash[:to] rule_hash = hash["rule"] || hash[:rule] requires_server = hash["requires_server"] || hash[:requires_server] || false rule = rule_hash ? Rules::Base.from_h(rule_hash) : nil new(target:, rule:, requires_server:) end |
Instance Method Details
#applies?(answers) ⇒ Boolean
Whether this transition should be taken given current answers.
35 36 37 38 39 |
# File 'lib/inquirex/transition.rb', line 35 def applies?(answers) return true if @rule.nil? @rule.evaluate(answers) end |
#condition_label ⇒ String
Human-readable label for the condition.
27 28 29 |
# File 'lib/inquirex/transition.rb', line 27 def condition_label @rule ? @rule.to_s : "always" end |
#to_h ⇒ Hash
Serializes to a plain Hash for JSON output.
44 45 46 47 48 49 |
# File 'lib/inquirex/transition.rb', line 44 def to_h hash = { "to" => @target.to_s } hash["rule"] = @rule.to_h if @rule hash["requires_server"] = true if @requires_server hash end |