Class: Inquirex::Answers

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

Overview

Structured wrapper around the answers collected during flow execution. Provides dot-notation access, bracket access, dig with dot-separated keys, and serialization helpers.

Examples:

answers = Inquirex::Answers.new({ filing_status: "single", business: { count: 3 } })
answers.filing_status          # => "single"
answers[:filing_status]        # => "single"
answers.business.count         # => 3 (nested dot-notation)
answers.dig("business.count")  # => 3
answers.to_flat_h              # => { "filing_status" => "single", "business.count" => 3 }

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Answers

Returns a new instance of Answers.

Parameters:

  • data (Hash) (defaults to: {})

    flat or nested hash of answers (symbol or string keys)



19
20
21
# File 'lib/inquirex/answers.rb', line 19

def initialize(data = {})
  @data = deep_symbolize(data).freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Dot-notation access.

Raises:

  • (NoMethodError)

    if the key does not exist (like a real method would)



34
35
36
37
38
# File 'lib/inquirex/answers.rb', line 34

def method_missing(name, *args)
  return super if args.any? || !@data.key?(name)

  wrap(@data[name])
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/inquirex/answers.rb', line 73

def ==(other)
  case other
  when Answers then @data == other.to_h
  when Hash    then @data == deep_symbolize(other)
  else false
  end
end

#[](key) ⇒ Object, ...

Bracket access (symbol or string key).

Parameters:

  • key (Symbol, String)

Returns:



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

def [](key)
  wrap(@data[key.to_sym])
end

#dig(*keys) ⇒ Object?

Dig using dot-separated key string or sequential keys.

Parameters:

  • keys (Array<String, Symbol>)

    path components or a single dot-separated string

Returns:

  • (Object, nil)


48
49
50
51
52
53
54
55
# File 'lib/inquirex/answers.rb', line 48

def dig(*keys)
  parts = keys.length == 1 ? keys.first.to_s.split(".").map(&:to_sym) : keys.map(&:to_sym)
  parts.reduce(@data) do |current, key|
    return nil unless current.is_a?(Hash)

    current[key]
  end
end

#empty?Boolean

Returns true when no answers have been collected.

Returns:

  • (Boolean)

    true when no answers have been collected



82
83
84
# File 'lib/inquirex/answers.rb', line 82

def empty?
  @data.empty?
end

#inspectObject



100
101
102
# File 'lib/inquirex/answers.rb', line 100

def inspect
  "#<Inquirex::Answers #{@data.inspect}>"
end

#merge(other) ⇒ Answers

Merge another hash or Answers into this one (returns new Answers instance).

Parameters:

Returns:



95
96
97
98
# File 'lib/inquirex/answers.rb', line 95

def merge(other)
  other_data = other.is_a?(Answers) ? other.to_h : deep_symbolize(other)
  Answers.new(@data.merge(other_data))
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/inquirex/answers.rb', line 40

def respond_to_missing?(name, include_private = false)
  @data.key?(name) || super
end

#sizeObject

Number of top-level answer keys.



87
88
89
# File 'lib/inquirex/answers.rb', line 87

def size
  @data.size
end

#to_flat_hHash

Returns flat hash with string dot-notation keys.

Returns:

  • (Hash)

    flat hash with string dot-notation keys



63
64
65
# File 'lib/inquirex/answers.rb', line 63

def to_flat_h
  flatten_hash(@data)
end

#to_hHash

Returns nested hash with symbol keys.

Returns:

  • (Hash)

    nested hash with symbol keys



58
59
60
# File 'lib/inquirex/answers.rb', line 58

def to_h
  @data.dup
end

#to_jsonString

Returns JSON representation.

Returns:

  • (String)

    JSON representation



68
69
70
# File 'lib/inquirex/answers.rb', line 68

def to_json(*)
  JSON.generate(stringify_keys(@data))
end