Class: ForemanMaintain::YamlStorage
- Inherits:
-
Object
- Object
- ForemanMaintain::YamlStorage
- Extended by:
- Forwardable
- Defined in:
- lib/foreman_maintain/yaml_storage.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#sub_key ⇒ Object
readonly
Returns the value of attribute sub_key.
Class Method Summary collapse
- .load(sub_key) ⇒ Object
- .load_file ⇒ Object
- .save_all ⇒ Object
- .save_sub_key(sub_key, data_val) ⇒ Object
- .storage_file_path ⇒ Object
- .storage_register ⇒ Object
Instance Method Summary collapse
-
#initialize(sub_key, data = {}) ⇒ YamlStorage
constructor
A new instance of YamlStorage.
- #save ⇒ Object
- #update_and_save(attributes) ⇒ Object
Constructor Details
#initialize(sub_key, data = {}) ⇒ YamlStorage
Returns a new instance of YamlStorage.
8 9 10 11 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 8 def initialize(sub_key, data = {}) @sub_key = sub_key @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 4 def data @data end |
#sub_key ⇒ Object (readonly)
Returns the value of attribute sub_key.
4 5 6 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 4 def sub_key @sub_key end |
Class Method Details
.load(sub_key) ⇒ Object
39 40 41 42 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 39 def load(sub_key) storage_register[sub_key] ||= load_file.fetch(sub_key, {}) YamlStorage.new(sub_key, storage_register[sub_key]) end |
.load_file ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 23 def load_file if File.exist?(storage_file_path) YAML.load_file(storage_file_path) || {} else {} end end |
.save_all ⇒ Object
49 50 51 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 49 def save_all storage_register.each_value(&:save) end |
.save_sub_key(sub_key, data_val) ⇒ Object
44 45 46 47 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 44 def save_sub_key(sub_key, data_val) new_data = load_file.merge(sub_key => data_val) File.open(storage_file_path, 'w') { |f| f.write new_data.to_yaml } end |
.storage_file_path ⇒ Object
31 32 33 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 31 def storage_file_path File.(ForemanMaintain.config.storage_file) end |
.storage_register ⇒ Object
35 36 37 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 35 def storage_register @storage_register ||= {} end |
Instance Method Details
#save ⇒ Object
13 14 15 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 13 def save self.class.save_sub_key(sub_key, data) end |
#update_and_save(attributes) ⇒ Object
17 18 19 20 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 17 def update_and_save(attributes) @data.merge!(attributes) save end |