Class: GraphQL::Query::LookupHash

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/query.rb

Overview

Caches items by name, raises an error if not found

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_name, items) ⇒ LookupHash

Returns a new instance of LookupHash.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/graphql/query.rb', line 93

def initialize(item_name, items)
  @items = items
  @item_name = item_name
  @storage = Hash.new do |hash, identifier|
    value = items.find {|i| i.identifier == identifier }
    if value.blank?
      "No #{item_name} found for #{identifier}, defined #{item_name}s are: #{items.map(&:identifier)}"
    end
    hash[identifier] = value
  end
end

Instance Attribute Details

#item_nameObject (readonly)

Returns the value of attribute item_name.



92
93
94
# File 'lib/graphql/query.rb', line 92

def item_name
  @item_name
end

#itemsObject (readonly)

Returns the value of attribute items.



92
93
94
# File 'lib/graphql/query.rb', line 92

def items
  @items
end

Instance Method Details

#[](identifier) ⇒ Object



105
106
107
# File 'lib/graphql/query.rb', line 105

def [](identifier)
  @storage[identifier]
end