module ResourceStruct::Extensions::IndifferentLookup

Common code between FirmStruct and LooseStruct

Public Class Methods

new(hash = {}) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 15
def initialize(hash = {})
  @hash = hash || {}
  @ro_struct = {}

  raise ::ArgumentError, "first argument must be a Hash, found #{@hash.class.name}" unless @hash.is_a?(Hash)
end

Public Instance Methods

==(other) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 26
def ==(other)
  other.is_a?(Hash) && ___all_keys_equal(other) ||
    (other.is_a?(LooseStruct) || other.is_a?(FirmStruct)) &&
      ___all_keys_equal(other.instance_variable_get(:@hash))
end
[](key, *sub_keys)
Alias for: dig
dig(key, *sub_keys) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 32
def dig(key, *sub_keys)
  ckey = ___convert_key(key)

  result =
    if @ro_struct.key?(ckey)
      @ro_struct[ckey]
    elsif @hash.key?(key)
      @ro_struct[ckey] = ___convert_value(@hash[key])
    elsif key.is_a?(String) && @hash.key?(key.to_sym)
      @ro_struct[ckey] = ___convert_value(@hash[key.to_sym])
    elsif key.is_a?(Symbol) && @hash.key?(key.to_s)
      @ro_struct[ckey] = ___convert_value(@hash[key.to_s])
    end

  return result if sub_keys.empty?

  return unless result

  raise TypeError, "#{result.class.name} does not have #dig method" unless result.respond_to?(:dig)

  result.dig(*sub_keys)
end
Also aliased as: []
inspect() click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 22
def inspect
  "#{self.class.name}<#{@hash.inspect}>"
end
marshal_dump() click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 56
def marshal_dump
  {
    data: @hash
  }
end
marshal_load(obj) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 62
def marshal_load(obj)
  @ro_struct = {}
  @hash = obj[:data]
end

Private Instance Methods

___all_keys_equal(other) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 90
def ___all_keys_equal(other)
  return false unless @hash.count == other.count

  @hash.reduce(true) do |acc, (k, _)|
    value = self[k]
    if other.key?(k)
      acc && value == other[k]
    elsif k.is_a?(String)
      ck = k.to_sym
      acc && other.key?(ck) && value == other[ck]
    else
      ck = ___convert_key(k)
      acc && other.key?(ck) && value == other[ck]
    end
  end
end
___convert_key(key) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 86
def ___convert_key(key)
  key.is_a?(::Symbol) ? key.to_s : key
end
___convert_value(value) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 69
def ___convert_value(value)
  case value
  when ::Array
    value.map { |v| ___convert_value(v) }.freeze
  when Hash
    self.class.new(value)
  else
    value
  end
end
___key?(key) click to toggle source
# File lib/resource_struct/extensions/indifferent_lookup.rb, line 80
def ___key?(key)
  @hash.key?(key) ||
    @hash.key?(___convert_key(key)) ||
    key.is_a?(String) && @hash.key?(key.to_sym)
end