Sha256: e5f19ff2b39ab81031cb51b511b285590b484c399a837987909d0e806c1db31f

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# A simpler wrapper to allows either String or Symbol keys to be used
# when accessing attributes since fog applies a change on the top
# level resulting in a mix of both which has introduced issues.
class IndifferentAccessHash
  def initialize(hash)
    @hash = hash
  end

  # @param key [String, Symbol] the key to look up
  # @return [Object] the value of the key
  def [](key)
    value = @hash[key.to_s] || @hash[key.to_sym]
    wrap(value)
  end

  # @param key [String, Symbol] the key to set
  # @param value [Object] the value to set
  # @return [Object] the value of the key
  def []=(key, value)
    @hash[key.to_sym] = value
  end

  # @param other [Object] the object to compare
  # @return [Object] the result of the comparison
  def ==(other)
    @hash == (other.is_a?(IndifferentAccessHash) ? other.to_h : other)
  end

  def method_missing(method, *args, &block)
    @hash.send(method, *args, &block)
  end

  def to_h
    @hash
  end

  private

  # This is to handle nested hashes to avoid the original issue again
  def wrap(value)
    case value
    when Hash
      IndifferentAccessHash.new(value)
    when Array
      value.map { |v| wrap(v) }
    else
      value
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
brightbox-cli-5.0.0 lib/brightbox-cli/indifferent_access_hash.rb
brightbox-cli-5.0.0.rc2 lib/brightbox-cli/indifferent_access_hash.rb
brightbox-cli-5.0.0.rc1 lib/brightbox-cli/indifferent_access_hash.rb