Class: Inquirex::UI::Node
- Inherits:
-
Node
- Object
- Node
- Inquirex::UI::Node
- 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
-
#widget_hints ⇒ Hash{Symbol => WidgetHint}?
readonly
Map of target → hint.
Class Method Summary collapse
-
.from_h(id, hash) ⇒ UI::Node
Deserializes from a plain Hash (string or symbol keys).
Instance Method Summary collapse
-
#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.
-
#initialize(widget_hints: nil) ⇒ Node
constructor
A new instance of Node.
-
#to_h ⇒ Hash
Serializes to a plain Hash.
-
#widget_hint_for(target: :desktop) ⇒ WidgetHint?
Returns the explicit hint for the given target, or nil when not set.
Constructor Details
#initialize(widget_hints: nil) ⇒ Node
Returns a new instance of 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 = &.freeze super(**) end |
Instance Attribute Details
#widget_hints ⇒ Hash{Symbol => WidgetHint}? (readonly)
Map of target → hint. nil for display nodes (say/header/btw/warning).
14 15 16 |
# File 'lib/inquirex/ui/node.rb', line 14 def @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.
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) = hash["widget"] || hash[:widget] = if .is_a?(Hash) && .any? { |_, v| v.is_a?(Hash) } # New format: { "desktop" => { "type" => "...", ... }, ... } .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] = 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 = () 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.
38 39 40 |
# File 'lib/inquirex/ui/node.rb', line 38 def (target: :desktop) (target:) || WidgetRegistry.default_hint_for(type, context: target) end |
#to_h ⇒ Hash
Serializes to a plain Hash. When widget_hints are present, they are nested under the “widget” key as { “desktop” => …, “mobile” => … }.
46 47 48 49 50 51 52 53 |
# File 'lib/inquirex/ui/node.rb', line 46 def to_h hash = super if && !.empty? hash["widget"] = .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.
29 30 31 |
# File 'lib/inquirex/ui/node.rb', line 29 def (target: :desktop) &.fetch(target.to_sym, nil) end |