Class: ForestAdminAgent::Utils::Schema::SchemaEmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_agent/utils/schema/schema_emitter.rb

Constant Summary collapse

LIANA_NAME =
"agent-ruby"
LIANA_VERSION =
"1.22.2"

Class Method Summary collapse

Class Method Details

.generate(datasource) ⇒ Object



11
12
13
14
15
# File 'lib/forest_admin_agent/utils/schema/schema_emitter.rb', line 11

def self.generate(datasource)
  datasource.collections
            .map { |_name, collection| GeneratorCollection.build_schema(collection) }
            .sort_by { |collection| collection[:name] }
end

.get_smart_features_by_collection(type, data, with_attributes: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/forest_admin_agent/utils/schema/schema_emitter.rb', line 62

def self.get_smart_features_by_collection(type, data, with_attributes: false)
  smart_features = []

  data.each do |value|
    smart_feature = { id: value[:id], type: type }
    smart_feature[:attributes] = value if with_attributes
    smart_features << smart_feature
  end

  smart_features
end

.metaObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/forest_admin_agent/utils/schema/schema_emitter.rb', line 17

def self.meta
  {
    liana: LIANA_NAME,
    liana_version: LIANA_VERSION,
    stack: {
      engine: 'ruby',
      engine_version: RUBY_VERSION
    }
  }
end

.serialize(schema) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/forest_admin_agent/utils/schema/schema_emitter.rb', line 28

def self.serialize(schema)
  hash = Digest::SHA1.hexdigest(schema[:collections].to_json)
  data = []
  included = []
  schema[:collections].each do |collection|
    collection_actions = collection[:actions]
    collection_segments = collection[:segments]

    collection.delete(:actions)
    collection.delete(:segments)

    included << get_smart_features_by_collection('actions', collection_actions, with_attributes: true)
    included << get_smart_features_by_collection('segments', collection_segments, with_attributes: true)

    data.push(
      {
        id: collection[:name],
        type: 'collections',
        attributes: collection,
        relationships: {
          actions: { data: get_smart_features_by_collection('actions', collection_actions) },
          segments: { data: get_smart_features_by_collection('segments', collection_segments) }
        }
      }
    )
  end

  {
    data: data,
    included: included.reject!(&:empty?)&.flatten || [],
    meta: schema[:meta].merge(schemaFileHash: hash)
  }
end