Class: Interfacets::Shared::GeneratedStore::Constructor

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/shared/generated_store.rb

Class Method Summary collapse

Class Method Details

.call(entity_class) ⇒ Object



7
8
9
10
11
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
# File 'lib/interfacets/shared/generated_store.rb', line 7

def self.call(entity_class)
  klass = Class.new(GeneratedStore) do
    define_singleton_method(:name) { "#{entity_class.name}.GeneratedStore" }
  end

  entity_class.accessors.each do |name, spec|
    klass.accessor(name)
  end

  entity_class.associations.each do |name, spec|
    sub_class = call(spec.klass)

    if spec.type == :reference
      klass.association(name, klass: sub_class)
    else
      klass.collection(name, klass: sub_class)
    end
  end

  entity_class.actions.each do |name, spec|
    klass.define_method(name) do |entity:|
      Client::System
        .current_bus
        .channel("interfacets:api")
        .builder
        .submit(name, nesting: entity.entity_nesting)
    end
  end

  klass
end