Class: WhatTheGem::Hobject
Class Method Summary collapse
Instance Method Summary collapse
- #deep_to_h ⇒ Object
-
#initialize(hash) ⇒ Hobject
constructor
A new instance of Hobject.
- #inspect ⇒ Object (also: #to_s)
- #merge(other) ⇒ Object
- #to_h ⇒ Object
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_h ⇒ Object
38 39 40 |
# File 'lib/whatthegem/hobject.rb', line 38 def deep_to_h @hash.transform_values(&method(:val_to_h)) end |
#inspect ⇒ Object 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_h ⇒ Object
30 31 32 |
# File 'lib/whatthegem/hobject.rb', line 30 def to_h @hash end |