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



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

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_api_configs_hash(cache_key = nil) ⇒ Object



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

def build_api_configs_hash(cache_key = nil)
  Motor::Configs::LoadFromCache.load_api_configs(cache_key: cache_key).sort_by(&:id).map do |config|
    config.slice(i[id name url preferences description]).merge(updated_at: config.updated_at.to_time)
  end
end

.build_configs_hash(cache_key = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/motor/configs/build_configs_hash.rb', line 58

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



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

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



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

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 api_config_name])
        .merge(tags: form.tags.map(&:name), updated_at: form.updated_at.to_time)
  end
end

.build_queries_hash(cache_key = nil) ⇒ Object



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

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



68
69
70
71
72
73
74
75
76
# File 'lib/motor/configs/build_configs_hash.rb', line 68

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
22
# 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]),
    api_configs: build_api_configs_hash(cache_keys[:api_configs])
  )
end

.normalize_hash(value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/motor/configs/build_configs_hash.rb', line 78

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