Module: Motor::Configs::BuildConfigsHash

Defined in:
lib/motor/configs/build_configs_hash.rb

Class Method Summary collapse

Class Method Details

.build_alerts_hash(cache_key = nil) ⇒ Object


37
38
39
40
41
42
# File 'lib/motor/configs/build_configs_hash.rb', line 37

def build_alerts_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_alerts(cache_key: cache_key).sort_by(&:id).map do |alert|
    alert.slice(%i[id name query_id to_emails is_enabled description preferences])
         .merge(tags: alert.tags.map(&:name), updated_at: alert.updated_at.to_time)
  end
end

.build_configs_hash(cache_key = nil) ⇒ Object


51
52
53
54
55
56
57
58
59
# File 'lib/motor/configs/build_configs_hash.rb', line 51

def build_configs_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_configs(cache_key: cache_key).sort_by(&:key).map do |config|
    {
      key: config.key,
      value: config.value,
      updated_at: config.updated_at.to_time
    }
  end
end

.build_dashboards_hash(cache_key = nil) ⇒ Object


30
31
32
33
34
35
# File 'lib/motor/configs/build_configs_hash.rb', line 30

def build_dashboards_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_dashboards(cache_key: cache_key).sort_by(&:id).map do |dashboard|
    dashboard.slice(%i[id title description preferences])
             .merge(tags: dashboard.tags.map(&:name), updated_at: dashboard.updated_at.to_time)
  end
end

.build_forms_hash(cache_key = nil) ⇒ Object


44
45
46
47
48
49
# File 'lib/motor/configs/build_configs_hash.rb', line 44

def build_forms_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_forms(cache_key: cache_key).sort_by(&:id).map do |form|
    form.slice(%i[id name http_method api_path description preferences])
        .merge(tags: form.tags.map(&:name), updated_at: form.updated_at.to_time)
  end
end

.build_queries_hash(cache_key = nil) ⇒ Object


23
24
25
26
27
28
# File 'lib/motor/configs/build_configs_hash.rb', line 23

def build_queries_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_queries(cache_key: cache_key).sort_by(&:id).map do |query|
    query.slice(%i[id name sql_body description preferences])
         .merge(tags: query.tags.map(&:name), updated_at: query.updated_at.to_time)
  end
end

.build_resources_hash(cache_key = nil) ⇒ Object


61
62
63
64
65
66
67
68
69
# File 'lib/motor/configs/build_configs_hash.rb', line 61

def build_resources_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_resources(cache_key: cache_key).sort_by(&:name).map do |resource|
    {
      name: resource.name,
      preferences: resource.preferences,
      updated_at: resource.updated_at.to_time
    }
  end
end

.callObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/motor/configs/build_configs_hash.rb', line 8

def call
  cache_keys = LoadFromCache.load_cache_keys

  normalize_hash(
    engine_version: Motor::VERSION,
    file_version: cache_keys.values.compact.max.to_time,
    resources: build_resources_hash(cache_keys[:resources]),
    configs: build_configs_hash(cache_keys[:configs]),
    queries: build_queries_hash(cache_keys[:queries]),
    dashboards: build_dashboards_hash(cache_keys[:dashboards]),
    forms: build_forms_hash(cache_keys[:forms]),
    alerts: build_alerts_hash(cache_keys[:alerts])
  )
end

.normalize_hash(value) ⇒ Object


71
72
73
74
75
76
77
78
79
80
# File 'lib/motor/configs/build_configs_hash.rb', line 71

def normalize_hash(value)
  case value
  when Hash, HashWithIndifferentAccess
    value.to_h.stringify_keys.transform_values { |v| normalize_hash(v) }
  when Array
    value.map { |e| normalize_hash(e) }
  else
    value
  end
end