Class: Inquirex::Answers
- Inherits:
-
Object
- Object
- Inquirex::Answers
- 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.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#[](key) ⇒ Object, ...
Bracket access (symbol or string key).
-
#dig(*keys) ⇒ Object?
Dig using dot-separated key string or sequential keys.
-
#empty? ⇒ Boolean
True when no answers have been collected.
-
#initialize(data = {}) ⇒ Answers
constructor
A new instance of Answers.
- #inspect ⇒ Object
-
#merge(other) ⇒ Answers
Merge another hash or Answers into this one (returns new Answers instance).
-
#method_missing(name, *args) ⇒ Object
Dot-notation access.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#size ⇒ Object
Number of top-level answer keys.
-
#to_flat_h ⇒ Hash
Flat hash with string dot-notation keys.
-
#to_h ⇒ Hash
Nested hash with symbol keys.
-
#to_json ⇒ String
JSON representation.
Constructor Details
#initialize(data = {}) ⇒ Answers
Returns a new instance of Answers.
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.
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
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).
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.
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.
82 83 84 |
# File 'lib/inquirex/answers.rb', line 82 def empty? @data.empty? end |
#inspect ⇒ Object
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).
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
40 41 42 |
# File 'lib/inquirex/answers.rb', line 40 def respond_to_missing?(name, include_private = false) @data.key?(name) || super end |
#size ⇒ Object
Number of top-level answer keys.
87 88 89 |
# File 'lib/inquirex/answers.rb', line 87 def size @data.size end |
#to_flat_h ⇒ Hash
Returns 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_h ⇒ Hash
Returns nested hash with symbol keys.
58 59 60 |
# File 'lib/inquirex/answers.rb', line 58 def to_h @data.dup end |
#to_json ⇒ String
Returns JSON representation.
68 69 70 |
# File 'lib/inquirex/answers.rb', line 68 def to_json(*) JSON.generate(stringify_keys(@data)) end |