123456789_123456789_123456789_123456789_123456789_

Class: GraphQL::Client::HashWithIndifferentAccess

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: lib/graphql/client/hash_with_indifferent_access.rb

Overview

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

Also see ActiveSupport::HashWithIndifferentAccess.

Class Method Summary

Instance Method Summary

Constructor Details

.new(hash = {}) ⇒ HashWithIndifferentAccess

[ GitHub ]

  
# 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

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 31

def [](key)
  @hash[convert_value(key)]
end

#convert_value(key) (private)

[ GitHub ]

  
# 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

#each_key(&block)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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?.

[ GitHub ]

  
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 43

alias has_key? key?

#include?(key)

Alias for #key?.

[ GitHub ]

  
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 42

alias include? key?

#key?(key) ⇒ Boolean Also known as: #include?, #has_key?, #member?

[ GitHub ]

  
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 39

def key?(key)
  @hash.key?(convert_value(key))
end

#member?(key)

Alias for #key?.

[ GitHub ]

  
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 44

alias member? key?