Class: Hashash::ObjectifiedHash
- Inherits:
-
Object
- Object
- Hashash::ObjectifiedHash
- Defined in:
- lib/hashash/objectified_hash.rb
Overview
Converts hashes to the objects.
Instance Method Summary collapse
-
#initialize(hash) ⇒ ObjectifiedHash
constructor
Creates a new ObjectifiedHash object.
-
#inspect ⇒ String
Formatted string with the class name, object id and original hash.
-
#method_missing(key) ⇒ Object
Delegate to ObjectifiedHash.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_hash ⇒ Hash
(also: #to_h)
The original hash.
Constructor Details
#initialize(hash) ⇒ ObjectifiedHash
Creates a new ObjectifiedHash object.
5 6 7 8 9 10 11 12 13 |
# File 'lib/hashash/objectified_hash.rb', line 5 def initialize(hash) @hash = hash @data = hash.each_with_object({}) do |(key, value), data| value = ObjectifiedHash.new(value) if value.is_a?(Hash) value = value.collect { |e| ObjectifiedHash.new(e) } if value.is_a?(Array) data[key.to_s] = value data end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key) ⇒ Object
Delegate to ObjectifiedHash.
27 28 29 |
# File 'lib/hashash/objectified_hash.rb', line 27 def method_missing(key) @data.key?(key.to_s) ? @data[key.to_s] : nil end |
Instance Method Details
#inspect ⇒ String
Returns Formatted string with the class name, object id and original hash.
22 23 24 |
# File 'lib/hashash/objectified_hash.rb', line 22 def inspect "#<#{self.class}:#{object_id} {hash: #{@hash.inspect}}" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
31 32 33 |
# File 'lib/hashash/objectified_hash.rb', line 31 def respond_to_missing?(method_name, include_private = false) @hash.keys.map(&:to_sym).include?(method_name.to_sym) || super end |
#to_hash ⇒ Hash Also known as: to_h
Returns The original hash.
16 17 18 |
# File 'lib/hashash/objectified_hash.rb', line 16 def to_hash @hash end |