Class: Summoner::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/summoner/sync.rb

Class Method Summary collapse

Class Method Details

.call(yaml_path: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/summoner/sync.rb', line 5

def self.call(yaml_path: nil)
  yaml_path ||= Rails.root.join('config', 'features.yml')

  unless File.exist?(yaml_path)
    Rails.logger.warn "[Summoner] No features.yml file found at #{yaml_path}. Skipping synchronization."
    return false
  end

  parsed_yaml = YAML.load_file(yaml_path) || {}

  Summoner::Feature.transaction do
    sync_feature(parsed_yaml)
    deactivate_missing_features(parsed_yaml)
  end

  true
end

.deactivate_missing_features(parsed_yaml) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/summoner/sync.rb', line 68

def self.deactivate_missing_features(parsed_yaml)
  active_keys = []

  parsed_yaml.each do |namespace, features|
    next unless features.is_a?(Hash)

    features.each do |raw_name, config|
      yaml_value = config['default']
      inferred_type = infer_type(yaml_value)
      name = normalize_name(raw_name, inferred_type)

      active_keys << "#{namespace}.#{name}"
    end
  end

  Summoner::Feature.where.not(key: active_keys).update_all(active: false)
end

.infer_type(value) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/summoner/sync.rb', line 86

def self.infer_type(value)
  case value
  when TrueClass, FalseClass then 'boolean'
  when Integer then'integer'
  when Float then 'float'
  when Array, Hash then 'json'
  else 'string' 
  end
end

.normalize_name(name, type) ⇒ Object



96
97
98
99
100
# File 'lib/summoner/sync.rb', line 96

def self.normalize_name(name, type)
  name_str = name.to_s

  type == 'boolean' ? "#{name_str.chomp('?')}?" : name_str
end

.should_update_value?(feature, yaml_value) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/summoner/sync.rb', line 60

def self.should_update_value?(feature, yaml_value)
  feature.new_record? || feature.last_yaml_value != yaml_value
end

.should_update_yaml_field?(feature, yaml_value, tracked_attribute) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/summoner/sync.rb', line 64

def self.should_update_yaml_field?(feature, yaml_value, tracked_attribute)
  feature.new_record? || feature.public_send(tracked_attribute) != yaml_value
end

.sync_feature(parsed_yaml) ⇒ Object



25
26
27
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
# File 'lib/summoner/sync.rb', line 25

def self.sync_feature(parsed_yaml)
  parsed_yaml.each do |namespace, features|
    next unless features.is_a?(Hash)

    features.each do |raw_name, config|
      yaml_value = config['default']
      yaml_description = config['description']
      inferred_type = infer_type(yaml_value)
      name = normalize_name(raw_name, inferred_type)
      key = "#{namespace}.#{name}"
      feature = Summoner::Feature.find_or_initialize_by(key: key)

      feature.assign_attributes(
        namespace: namespace,
        name: name,
        value_type: inferred_type,
        match_attribute: config['match_attribute'],
        active: true
      )

      if should_update_value?(feature, yaml_value)
        feature.default_value = yaml_value
        feature.last_yaml_value = yaml_value
      end

      if should_update_yaml_field?(feature, yaml_description, :last_yaml_description)
        feature.description = yaml_description
        feature.last_yaml_description = yaml_description
      end

      feature.save!
    end
  end
end