Class: WhatTheGem::Hobject

Inherits:
Object show all
Defined in:
lib/whatthegem/hobject.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Hobject

Returns a new instance of Hobject.



22
23
24
25
26
27
28
# 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

Class Method Details

.deep(hash_or_array) ⇒ Object



4
5
6
# File 'lib/whatthegem/hobject.rb', line 4

def deep(hash_or_array)
  deep_value(hash_or_array)
end

Instance Method Details

#deep_to_hObject



38
39
40
# File 'lib/whatthegem/hobject.rb', line 38

def deep_to_h
  @hash.transform_values(&method(:val_to_h))
end

#inspectObject Also known as: to_s



42
43
44
# File 'lib/whatthegem/hobject.rb', line 42

def inspect
  '#<Hobject(%s)>' % @hash.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
end

#merge(other) ⇒ Object



34
35
36
# File 'lib/whatthegem/hobject.rb', line 34

def merge(other)
  Hobject.new(to_h.merge(other.to_h))
end

#to_hObject



30
31
32
# File 'lib/whatthegem/hobject.rb', line 30

def to_h
  @hash
end