class GraphQL::Client::HashWithIndifferentAccess

Public: Implements a read only hash where keys can be accessed by strings, symbols, snake or camel case.

Also see ActiveSupport::HashWithIndifferentAccess.

Public Class Methods

new(hash = {}) click to toggle source
# File lib/graphql/client/hash_with_indifferent_access.rb, line 15
def initialize(hash = {})
  @hash = hash
  @aliases = {}

  hash.each_key do |key|
    if key.is_a?(String)
      key_alias = ActiveSupport::Inflector.underscore(key)
      @aliases[key_alias] = key if key != key_alias
    end
  end

  freeze
end

Public Instance Methods

[](key) click to toggle source
# File lib/graphql/client/hash_with_indifferent_access.rb, line 31
def [](key)
  @hash[convert_value(key)]
end
each_key() { |convert_value(key)| ... } click to toggle source
# File lib/graphql/client/hash_with_indifferent_access.rb, line 46
def each_key(&block)
  @hash.each_key { |key| yield convert_value(key) }
end
fetch(key, *args, &block) click to toggle source
# File lib/graphql/client/hash_with_indifferent_access.rb, line 35
def fetch(key, *args, &block)
  @hash.fetch(convert_value(key), *args, &block)
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/graphql/client/hash_with_indifferent_access.rb, line 39
def key?(key)
  @hash.key?(convert_value(key))
end
Also aliased as: include?, has_key?, member?
member?(key)
Alias for: key?

Private Instance Methods

convert_value(key) click to toggle source
# File lib/graphql/client/hash_with_indifferent_access.rb, line 52
def convert_value(key)
  case key
  when String, Symbol
    key = key.to_s
    @aliases.fetch(key, key)
  else
    key
  end
end