Class: Gitlab::ObjectifiedHash

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

Overview

Converts hashes to the objects.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ObjectifiedHash

Creates a new ObjectifiedHash object.



5
6
7
8
9
10
11
12
# File 'lib/gitlab/objectified_hash.rb', line 5

def initialize(hash)
  @hash = hash
  @data = hash.inject({}) do |data, (key, value)|
    value = ObjectifiedHash.new(value) if value.is_a? Hash
    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.



26
27
28
# File 'lib/gitlab/objectified_hash.rb', line 26

def method_missing(key)
  @data.key?(key.to_s) ? @data[key.to_s] : nil
end

Instance Method Details

#inspectString

Returns Formatted string with the class name, object id and original hash.

Returns:

  • (String)

    Formatted string with the class name, object id and original hash.



21
22
23
# File 'lib/gitlab/objectified_hash.rb', line 21

def inspect
  "#<#{self.class}:#{object_id} {hash: #{@hash.inspect}}"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/gitlab/objectified_hash.rb', line 30

def respond_to_missing?(method_name, include_private = false)
  @hash.keys.map(&:to_sym).include?(method_name.to_sym) || super
end

#to_hashHash Also known as: to_h

Returns The original hash.

Returns:

  • (Hash)

    The original hash.



15
16
17
# File 'lib/gitlab/objectified_hash.rb', line 15

def to_hash
  @hash
end