class Gitlab::ObjectifiedHash

Converts hashes to the objects.

Attributes

data[R]
hash[R]

Public Class Methods

new(hash) click to toggle source

Creates a new ObjectifiedHash object.

# File lib/gitlab/objectified_hash.rb, line 7
def initialize(hash)
  @hash = hash
  @data = hash.each_with_object({}) do |(key, value), data|
    value = self.class.new(value) if value.is_a? Hash
    value = value.map { |v| v.is_a?(Hash) ? self.class.new(v) : v } if value.is_a? Array
    data[key.to_s] = value
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/gitlab/objectified_hash.rb, line 27
def [](key)
  data[key]
end
inspect() click to toggle source

@return [String] Formatted string with the class name, object id and original hash.

# File lib/gitlab/objectified_hash.rb, line 23
def inspect
  "#<#{self.class}:#{object_id} {hash: #{hash.inspect}}"
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

@return [Hash] The original hash.

# File lib/gitlab/objectified_hash.rb, line 17
def to_hash
  hash
end
Also aliased as: to_h

Private Instance Methods

method_missing(method_name, *args, &block) click to toggle source

Respond to messages for which `self.data` has a key

Calls superclass method
# File lib/gitlab/objectified_hash.rb, line 36
def method_missing(method_name, *args, &block)
  if data.key?(method_name.to_s)
    data[method_name.to_s]
  elsif data.respond_to?(method_name)
    warn 'WARNING: Please convert ObjectifiedHash object to hash before calling Hash methods on it.'
    data.send(method_name, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/gitlab/objectified_hash.rb, line 47
def respond_to_missing?(method_name, include_private = false)
  hash.keys.map(&:to_sym).include?(method_name.to_sym) || super
end