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.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql/client/hash_with_indifferent_access.rb', line 14

def initialize(hash = {})
  @hash = hash
  @aliases = {}

  hash.keys.each 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



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

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

#fetch(key, *args, &block) ⇒ Object



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

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)


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

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