Class: EasyHash::HashToObject

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HashToObject

Returns a new instance of HashToObject.



9
10
11
# File 'lib/easy_hash.rb', line 9

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/easy_hash.rb', line 13

def method_missing(method, *)
  hash_object = hash[method.to_sym]
  return super if hash_object.nil?
  return self.class.new(hash_object) if hash_object.is_a?(Hash)
  return convert_to_array_of_objects(hash_object) if hash_object.is_a?(Array)

  hash_object
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



7
8
9
# File 'lib/easy_hash.rb', line 7

def hash
  @hash
end

Instance Method Details

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/easy_hash.rb', line 22

def respond_to_missing?(method, *)
  return super if hash.nil? || hash[method.to_sym].nil?

  hash[method.to_sym]
end

#to_hObject



28
29
30
# File 'lib/easy_hash.rb', line 28

def to_h
  hash
end