Class: OpenStruct

Inherits:
Object show all
Defined in:
lib/spectre/helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_to!(defaults = {}, **kwargs) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/spectre/helpers.rb', line 103

def default_to! defaults = {}, **kwargs
  defaults = kwargs if kwargs.any?

  defaults.each_key do |key|
    self[key] = defaults[key] unless to_h.key? key
  end
end

#eachObject



93
94
95
# File 'lib/spectre/helpers.rb', line 93

def each(&)
  to_h.each(&)
end

#to_jsonObject



97
98
99
100
101
# File 'lib/spectre/helpers.rb', line 97

def to_json(*, **)
  to_h
    .transform_values { |x| x.is_a?(OpenStruct) ? x.to_h : x }
    .to_json(*, **)
end