Class: MidwireCommon::YamlSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/midwire_common/yaml_setting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ YamlSetting

Returns a new instance of YamlSetting.



7
8
9
# File 'lib/midwire_common/yaml_setting.rb', line 7

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/midwire_common/yaml_setting.rb', line 5

def file
  @file
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
24
# File 'lib/midwire_common/yaml_setting.rb', line 21

def [](key)
  load
  @config[key]
end

#[]=(key, value) ⇒ Object



26
27
28
29
# File 'lib/midwire_common/yaml_setting.rb', line 26

def []=(key, value)
  load
  @config[key] = value
end

#loadObject



11
12
13
14
# File 'lib/midwire_common/yaml_setting.rb', line 11

def load
  @config ||= YAML.load_file(file) || {}
  self
end

#saveObject



16
17
18
19
# File 'lib/midwire_common/yaml_setting.rb', line 16

def save
  File.open(file, 'w') { |f| f.write(YAML.dump(@config)) }
  self
end