Class: Igniter::Store::Protocol::Handlers::RelationHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/store/protocol/handlers/relation_handler.rb

Constant Summary collapse

REQUIRED =
%i[name from to].freeze

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ RelationHandler

Returns a new instance of RelationHandler.



10
# File 'lib/igniter/store/protocol/handlers/relation_handler.rb', line 10

def initialize(store) = @store = store

Instance Method Details

#call(descriptor) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/igniter/store/protocol/handlers/relation_handler.rb', line 12

def call(descriptor)
  missing = REQUIRED.select { |f| descriptor[f].nil? }
  return Receipt.rejection("Missing required fields: #{missing.join(", ")}", kind: :relation) if missing.any?

  name       = descriptor[:name].to_sym
  from_desc  = descriptor[:from]
  to_desc    = descriptor[:to]

  unless from_desc.is_a?(Hash) && from_desc[:store] && from_desc[:key]
    return Receipt.rejection("from: must be { store:, key: }", kind: :relation, name: name)
  end
  unless to_desc.is_a?(Hash) && to_desc[:store] && to_desc[:field]
    return Receipt.rejection("to: must be { store:, field: }", kind: :relation, name: name)
  end

  cardinality = descriptor.fetch(:cardinality, :many)
  warnings    = cardinality == :one ? ["cardinality: :one is informational only; engine always stores as G-Set"] : []

  @store.register_relation(
    name,
    source:    to_desc[:store].to_sym,
    partition: to_desc[:field].to_sym,
    target:    from_desc[:store].to_sym
  )

  Receipt.accepted(kind: :relation, name: name, warnings: warnings)
end