Class: Rails::GraphQL::Helpers::InheritedCollection::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/rails/graphql/helpers/inherited_collection/hash.rb

Overview

A inherited collection of Hash that can contain simple values, arrays, or sets

Instance Method Summary collapse

Methods inherited from Base

handle, #initialize

Constructor Details

This class inherits a constructor from Rails::GraphQL::Helpers::InheritedCollection::Base

Instance Method Details

#[](key) ⇒ Object

A simple way to get the result of one single key



54
55
56
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 54

def [](key)
  lazy[key]
end

#eachObject

Go over each key and value



21
22
23
24
25
26
27
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 21

def each
  Enumerator::Lazy.new(keys) do |yielder, key|
    val = self[key]
    yield(key, val) if block_given?
    yielder.yield(key, val)
  end
end

#each_key(&block) ⇒ Object

Go over each key exclusively



39
40
41
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 39

def each_key(&block)
  keys.each(&block)
end

#each_value(&block) ⇒ Object

Go over each value exclusively



30
31
32
33
34
35
36
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 30

def each_value(&block)
  Enumerator::Lazy.new(keys) do |yielder, key|
    val = self[key]
    yield(val) if block_given?
    yielder.yield(val)
  end
end

#key?(value) ⇒ Boolean

Simply go over each definition and check for the given key

Returns:

  • (Boolean)


11
12
13
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 11

def key?(value)
  each_definition.any? { |definition| definition.key?(value) }
end

#keysObject

Collect all the keys of all the definitions



16
17
18
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 16

def keys
  each_definition.reverse_each.flat_map(&:keys).uniq
end

#lazyObject

Build a lazy hash



59
60
61
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 59

def lazy
  @table ||= Hash.new(&method(_combine? ? :_combined_hash : :_simple_hash))
end

#to_hashObject

Basically returns the plain result value



49
50
51
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 49

def to_hash
  each.to_h
end

#valuesObject

Get all the values by lazy iterating over all the keys



44
45
46
# File 'lib/rails/graphql/helpers/inherited_collection/hash.rb', line 44

def values
  keys.lazy.map(&method(:[]))
end