Class: Orchestration::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestration/settings.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Settings

Returns a new instance of Settings.



5
6
7
8
9
# File 'lib/orchestration/settings.rb', line 5

def initialize(path)
  @path = path
  @dirty = false
  @exist = File.exist?(path)
end

Instance Method Details

#dirty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/orchestration/settings.rb', line 28

def dirty?
  @dirty
end

#exist?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/orchestration/settings.rb', line 32

def exist?
  @exist
end

#get(identifier) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/orchestration/settings.rb', line 11

def get(identifier)
  identifier.to_s.split('.').reduce(config) do |result, key|
    (result || {}).fetch(key)
  end
rescue KeyError
  nil
end

#set(identifier, val) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/orchestration/settings.rb', line 19

def set(identifier, val)
  *keys, setting_key = identifier.to_s.split('.')
  new_config = config || {}
  parent = keys.reduce(new_config) { |result, key| result[key] ||= {} }
  parent[setting_key] = val
  @dirty ||= config != new_config
  File.write(@path, new_config.to_yaml)
end