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.



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

def initialize(file)
  @file = file
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#dataObject



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

def data
  load
  config
end

#loadObject



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

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

#saveObject



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

def save
  File.write(file, YAML.dump(config))
  self
end