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.



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

def initialize(file)
  @file = file
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#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



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

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

#[]=(key, value) ⇒ Object



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

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

#dataObject



32
33
34
35
# File 'lib/midwire_common/yaml_setting.rb', line 32

def data
  load
  config
end

#loadObject



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

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

#saveObject



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

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