Class: MidwireCommon::YamlSetting
- Inherits:
-
Object
- Object
- MidwireCommon::YamlSetting
- Defined in:
- lib/midwire_common/yaml_setting.rb
Overview
Simple class for YAML settings
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #data ⇒ Object
-
#initialize(file) ⇒ YamlSetting
constructor
A new instance of YamlSetting.
- #load ⇒ Object
- #save ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/midwire_common/yaml_setting.rb', line 7 def config @config end |
#file ⇒ Object (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 |
#data ⇒ Object
33 34 35 36 |
# File 'lib/midwire_common/yaml_setting.rb', line 33 def data load config end |
#load ⇒ Object
13 14 15 16 |
# File 'lib/midwire_common/yaml_setting.rb', line 13 def load @config ||= YAML.load_file(file) || {} self end |
#save ⇒ Object
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 |