Class: MidwireCommon::YamlSetting

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

Overview

Simple class for YAML settings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ YamlSetting

Returns a new instance of YamlSetting.



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

def initialize(file)
  @file = file
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#dataObject



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

def data
  load
  config
end

#loadObject



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

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

#saveObject



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

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