Class: CaptainConfig::Service

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/captain_config/service.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(load_after_initialize: true, &block) ⇒ Service

Returns a new instance of Service.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/captain_config/service.rb', line 20

def initialize(load_after_initialize: true, &block)
  @configured_entries = {}
  @configs = Concurrent::ThreadLocalVar.new(nil)

  DSL.new(self).instance_eval(&block)

  # The last service to be initialized will always win.
  self.class.last_created_service = self

  # Make the service immediately available after it's declared.
  load if load_after_initialize
end

Instance Attribute Details

#configsObject (readonly)

The keys and values read from the database (or defaults).



13
14
15
# File 'lib/captain_config/service.rb', line 13

def configs
  @configs
end

#configured_entriesObject (readonly)

The statically-defined entries that this service should support.



10
11
12
# File 'lib/captain_config/service.rb', line 10

def configured_entries
  @configured_entries
end

Instance Method Details

#<<(configured_entry) ⇒ Object



33
34
35
# File 'lib/captain_config/service.rb', line 33

def <<(configured_entry)
  @configured_entries[configured_entry.key] = configured_entry
end

#get(key) ⇒ Object Also known as: []



37
38
39
40
41
# File 'lib/captain_config/service.rb', line 37

def get(key)
  assert_loaded!

  configs.value.fetch key
end

#loadObject

Load new values from the database (or default values from the configured entries).



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/captain_config/service.rb', line 66

def load
  new_configs = {}

  records = CaptainConfig::BaseConfig
    .where(key: configured_entries.keys)
    .map { |record| [record.key.to_sym, record] }
    .to_h

  configured_entries.each do |key, configured_entry|
    record = records[key]

    value = if record
      # Check that it's the type we're expecting.
      actual = record.class
      expected = configured_entry.model
      if expected != actual
        raise(
          "Error loading #{key}: record class from database doesn't match " \
          "configured class (expected: #{expected}, got: #{actual})",
        )
      end
      record.value
    else
      configured_entry.default_value.dup
    end

    new_configs[key] = value
  end

  @configs.value = new_configs
end

#loaded?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/captain_config/service.rb', line 60

def loaded?
  !configs.value.nil?
end

#set(key, new_value, coerce: true) ⇒ Object Also known as: []=



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/captain_config/service.rb', line 43

def set(key, new_value, coerce: true)
  assert_loaded!

  record = configured_entries.fetch(key).model.find_or_create_by!(key: key)
  record.value = coerce ? record.class.coerce(new_value) : new_value
  record.save!

  # Read the value back out of the record because the model/record
  # is authoritative.
  authoritative_new_value = record.value
  configs.value[key] = authoritative_new_value
  authoritative_new_value
end