Class: GraphitiGql::Schema::Registry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/graphiti_gql/schema/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



6
7
8
# File 'lib/graphiti_gql/schema/registry.rb', line 6

def initialize
  clear
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
# File 'lib/graphiti_gql/schema/registry.rb', line 40

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



36
37
38
# File 'lib/graphiti_gql/schema/registry.rb', line 36

def []=(key, value)
  @data[key] = value
end

#clearObject



32
33
34
# File 'lib/graphiti_gql/schema/registry.rb', line 32

def clear
  @data = {}
end

#get(object, interface: true) ⇒ Object



10
11
12
# File 'lib/graphiti_gql/schema/registry.rb', line 10

def get(object, interface: true)
  @data[key_for(object, interface: interface)]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/graphiti_gql/schema/registry.rb', line 44

def key?(key)
  @data.key?(key)
end

#key_for(object, interface: true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphiti_gql/schema/registry.rb', line 18

def key_for(object, interface: true)
  if object.ancestors.include?(Graphiti::Resource) 
    key = key_for_resource(object)
    if object.polymorphic?
      if !object.polymorphic_child? && interface
        key = "I#{key}"
      end
    end
    key
  else
    raise 'unknown object!'
  end
end

#resource_types(value_objects: true) ⇒ Object

When polymorphic parent, returns the Interface not the Class



53
54
55
56
57
58
59
# File 'lib/graphiti_gql/schema/registry.rb', line 53

def resource_types(value_objects: true)
  values
    .select { |v| v.key?(:resource) }
    .reject { |v| v[:interface] }
    .reject { |v| !value_objects && v[:resource].value_object? }
    .map { |registered| get(registered[:resource]) }
end

#set(resource, type, interface: true) ⇒ Object



14
15
16
# File 'lib/graphiti_gql/schema/registry.rb', line 14

def set(resource, type, interface: true)
  @data[key_for(resource, interface: interface)] = { resource: resource, type: type, interface: interface }
end

#valuesObject



48
49
50
# File 'lib/graphiti_gql/schema/registry.rb', line 48

def values
  @data.values
end