Class: Inquirex::UI::Node

Inherits:
Node
  • Object
show all
Defined in:
lib/inquirex/ui/node.rb

Overview

Enriched node that extends Inquirex::Node with widget rendering hints. Hints are stored as a Hash keyed by target context (e.g. :desktop, :mobile). Additional targets can be introduced by adapters without changing this class.

The hints are included in JSON serialization and consumed by frontend adapters (JS widget, TTY renderer, etc.). This class does NOT render anything itself.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widget_hints: nil) ⇒ Node

Returns a new instance of Node.

Parameters:

  • widget_hints (Hash{Symbol => WidgetHint}, nil) (defaults to: nil)
  • kwargs (Hash)

    all other params forwarded to Inquirex::Node



19
20
21
22
23
# File 'lib/inquirex/ui/node.rb', line 19

def initialize(widget_hints: nil, **)
  # Set widget attr BEFORE calling super — super calls freeze.
  @widget_hints = widget_hints&.freeze
  super(**)
end

Instance Attribute Details

#widget_hintsHash{Symbol => WidgetHint}? (readonly)

Map of target → hint. nil for display nodes (say/header/btw/warning).

Returns:

  • (Hash{Symbol => WidgetHint}, nil)

    the current value of widget_hints



14
15
16
# File 'lib/inquirex/ui/node.rb', line 14

def widget_hints
  @widget_hints
end

Class Method Details

.from_h(id, hash) ⇒ UI::Node

Deserializes from a plain Hash (string or symbol keys). Parses the nested “widget” target map in addition to all base Node fields.

Parameters:

  • id (Symbol, String)
  • hash (Hash)

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/inquirex/ui/node.rb', line 61

def self.from_h(id, hash)
  widget_data = hash["widget"] || hash[:widget]

  widget_hints =
    if widget_data.is_a?(Hash) && widget_data.any? { |_, v| v.is_a?(Hash) }
      # New format: { "desktop" => { "type" => "...", ... }, ... }
      widget_data.each_with_object({}) do |(target, hint_hash), acc|
        acc[target.to_sym] = WidgetHint.from_h(hint_hash)
      end
    end

  verb             = hash["verb"]        || hash[:verb]
  type             = hash["type"]        || hash[:type]
  question         = hash["question"]    || hash[:question]
  text             = hash["text"]        || hash[:text]
  raw_options      = hash["options"]     || hash[:options]
  transitions_data = hash["transitions"] || hash[:transitions] || []
  skip_if_data     = hash["skip_if"]     || hash[:skip_if]
  default          = hash["default"]     || hash[:default]

  transitions = transitions_data.map { |t| Inquirex::Transition.from_h(t) }
  skip_if     = skip_if_data ? Inquirex::Rules::Base.from_h(skip_if_data) : nil
  options     = deserialize_options(raw_options)

  new(
    id:,
    verb:,
    type:,
    question:,
    text:,
    options:,
    transitions:,
    skip_if:,
    default:,
    widget_hints:
  )
end

Instance Method Details

#effective_widget_hint_for(target: :desktop) ⇒ WidgetHint?

Returns the explicit hint for the given target, falling back to the registry default for this node’s type when no explicit hint is set.

Parameters:

  • target (Symbol) (defaults to: :desktop)

    e.g. :desktop, :mobile

Returns:



38
39
40
# File 'lib/inquirex/ui/node.rb', line 38

def effective_widget_hint_for(target: :desktop)
  widget_hint_for(target:) || WidgetRegistry.default_hint_for(type, context: target)
end

#to_hHash

Serializes to a plain Hash. When widget_hints are present, they are nested under the “widget” key as { “desktop” => …, “mobile” => … }.

Returns:

  • (Hash)


46
47
48
49
50
51
52
53
# File 'lib/inquirex/ui/node.rb', line 46

def to_h
  hash = super
  if widget_hints && !widget_hints.empty?
    hash["widget"] = widget_hints.transform_keys(&:to_s)
                                 .transform_values(&:to_h)
  end
  hash
end

#widget_hint_for(target: :desktop) ⇒ WidgetHint?

Returns the explicit hint for the given target, or nil when not set.

Parameters:

  • target (Symbol) (defaults to: :desktop)

    e.g. :desktop, :mobile

Returns:



29
30
31
# File 'lib/inquirex/ui/node.rb', line 29

def widget_hint_for(target: :desktop)
  widget_hints&.fetch(target.to_sym, nil)
end