Class: Inquirex::CompletionMetadata

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/inquirex/completion_metadata.rb

Overview

An OpenStruct describing how a flow reached completion: which rendering engine collected the answers, and any environment details that renderer chose to attach (host uname, user, IP addresses, terminal, ...).

Only :engine and :engine_version are required members — enforced at construction. Everything else is free-form OpenStruct behavior: unset members read as nil, assignment creates members, nested OpenStructs (e.g. uname) are welcome.

Examples:

meta = Inquirex::CompletionMetadata.new(
  engine: "inquirex-tty", engine_version: "0.5.0",
  uname: OpenStruct.new(Etc.uname)
)
meta.engine          # => "inquirex-tty"
meta.uname.machine   # => "arm64"
meta.hostname        # => nil (unset members read as nil)
meta.to_h            # => plain nested Hash, JSON-ready

Constant Summary collapse

REQUIRED_MEMBERS =

Members that must be provided at construction; everything else is optional.

%i[engine engine_version].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine:, engine_version:, **extra) ⇒ CompletionMetadata

Exists solely to make :engine and :engine_version required keywords — OpenStruct itself would accept anything.

Parameters:

  • engine (String)

    rendering front-end name (e.g. "inquirex-tty")

  • engine_version (String)

    rendering front-end version

  • extra (Hash)

    any additional, optional members



35
36
37
# File 'lib/inquirex/completion_metadata.rb', line 35

def initialize(engine:, engine_version:, **extra) # rubocop:disable Lint/UselessMethodDefinition
  super
end

Class Method Details

.from_h(hash) ⇒ CompletionMetadata?

Rebuilds an instance from a hash (e.g. persisted engine state after a JSON round-trip). Keys may be strings or symbols; nested hashes come back as OpenStructs so dot-access survives the round-trip.

Parameters:

  • hash (Hash, nil)

Returns:

Raises:

  • (ArgumentError)

    when :engine or :engine_version is missing



46
47
48
49
50
51
# File 'lib/inquirex/completion_metadata.rb', line 46

def self.from_h(hash)
  return nil if hash.nil? || hash.empty?

  members = hash.to_h { |k, v| [k.to_sym, v.is_a?(Hash) ? OpenStruct.new(v) : v] }
  new(**members)
end

Instance Method Details

#inspectString

Returns debug representation of the deep-plain member hash.

Returns:

  • (String)

    debug representation of the deep-plain member hash



67
68
69
# File 'lib/inquirex/completion_metadata.rb', line 67

def inspect
  "#<Inquirex::CompletionMetadata #{to_h.inspect}>"
end

#to_hHash

OpenStruct#to_h is shallow; deep-convert nested OpenStructs (uname et al.) so state serialization and JSON output stay plain data.

Returns:

  • (Hash)


57
58
59
# File 'lib/inquirex/completion_metadata.rb', line 57

def to_h
  deep_plain(super)
end

#to_jsonString

Returns JSON representation.

Returns:

  • (String)

    JSON representation



62
63
64
# File 'lib/inquirex/completion_metadata.rb', line 62

def to_json(*)
  JSON.generate(to_h)
end