Class: GraphQL::Client::HashWithIndifferentAccess

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
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.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.


15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object


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

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

#each_key(&block) ⇒ Object


46
47
48
# 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) ⇒ Object


35
36
37
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 35

def fetch(key, *args, &block)
  @hash.fetch(convert_value(key), *args, &block)
end

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

Returns:

  • (Boolean)

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

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