Class: CaptainConfig::Service
- Inherits:
-
Object
- Object
- CaptainConfig::Service
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/captain_config/service.rb
Defined Under Namespace
Classes: DSL
Instance Attribute Summary collapse
-
#configs ⇒ Object
readonly
The keys and values read from the database (or defaults).
-
#configured_entries ⇒ Object
readonly
The statically-defined entries that this service should support.
Instance Method Summary collapse
- #<<(configured_entry) ⇒ Object
- #get(key) ⇒ Object (also: #[])
-
#initialize(load_after_initialize: true, &block) ⇒ Service
constructor
A new instance of Service.
-
#load ⇒ Object
Load new values from the database (or default values from the configured entries).
- #loaded? ⇒ Boolean
- #set(key, new_value, coerce: true) ⇒ Object (also: #[]=)
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
#configs ⇒ Object (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_entries ⇒ Object (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 |
#load ⇒ Object
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
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. = record.value configs.value[key] = end |