Class: Inquirex::Transition

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, rule: nil, requires_server: false) ⇒ Transition

Returns a new instance of Transition.

Parameters:

  • target (Symbol)

    next step id

  • rule (Rules::Base, nil) (defaults to: nil)

    optional condition (nil = always)

  • requires_server (Boolean) (defaults to: false)

    whether this edge needs server evaluation



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_serverBoolean (readonly)

whether this transition needs a server round-trip

Returns:

  • (Boolean)

    the current value of requires_server



11
12
13
# File 'lib/inquirex/transition.rb', line 11

def requires_server
  @requires_server
end

#ruleRules::Base? (readonly)

condition; nil means unconditional (always applies)

Returns:



11
12
13
# File 'lib/inquirex/transition.rb', line 11

def rule
  @rule
end

#targetSymbol (readonly)

id of the step to go to when this transition applies

Returns:

  • (Symbol)

    the current value of target



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.

Parameters:

  • hash (Hash)

    with string or symbol keys

Returns:



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.

Parameters:

  • answers (Hash)

    current answer state

Returns:

  • (Boolean)

    true if rule is nil or rule evaluates to true



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_labelString

Human-readable label for the condition.

Returns:

  • (String)

    rule#to_s or “always”



27
28
29
# File 'lib/inquirex/transition.rb', line 27

def condition_label
  @rule ? @rule.to_s : "always"
end

#to_hHash

Serializes to a plain Hash for JSON output.

Returns:

  • (Hash)


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