class WhatTheGem::Hobject

Public Class Methods

deep(hash_or_array) click to toggle source
# File lib/whatthegem/hobject.rb, line 4
def deep(hash_or_array)
  deep_value(hash_or_array)
end
new(hash) click to toggle source
# File lib/whatthegem/hobject.rb, line 22
def initialize(hash)
  @hash = hash.transform_keys(&:to_sym).freeze
  @hash.each do |key, val|
    define_singleton_method(key) { val }
    define_singleton_method("#{key}?") { !!val }
  end
end

Private Class Methods

deep_value(val) click to toggle source
# File lib/whatthegem/hobject.rb, line 10
def deep_value(val)
  case
  when val.respond_to?(:to_hash)
    val.to_hash.transform_values(&method(:deep_value)).then(&method(:new))
  when val.respond_to?(:to_ary)
    val.to_ary.map(&method(:deep_value))
  else
    val
  end
end

Public Instance Methods

deep_to_h() click to toggle source
# File lib/whatthegem/hobject.rb, line 38
def deep_to_h
  @hash.transform_values(&method(:val_to_h))
end
inspect() click to toggle source
# File lib/whatthegem/hobject.rb, line 42
def inspect
  '#<Hobject(%s)>' % @hash.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
end
Also aliased as: to_s
merge(other) click to toggle source
# File lib/whatthegem/hobject.rb, line 34
def merge(other)
  Hobject.new(to_h.merge(other.to_h))
end
to_h() click to toggle source
# File lib/whatthegem/hobject.rb, line 30
def to_h
  @hash
end
to_s()
Alias for: inspect

Private Instance Methods

val_to_h(value) click to toggle source
# File lib/whatthegem/hobject.rb, line 50
def val_to_h(value)
  case value
  when Hobject
    value.deep_to_h
  when Array
    value.map(&method(:val_to_h))
  else
    value
  end
end