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.
7 8 9 10 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 7 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
38 39 40 41 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 38 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
22 23 24 25 26 27 28 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 22 def load_file if File.exist?(storage_file_path) YAML.load_file(storage_file_path) || {} else {} end end |
.save_all ⇒ Object
48 49 50 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 48 def save_all storage_register.values.each(&:save) end |
.save_sub_key(sub_key, data_val) ⇒ Object
43 44 45 46 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 43 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
30 31 32 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 30 def storage_file_path File.(ForemanMaintain.config.storage_file) end |
.storage_register ⇒ Object
34 35 36 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 34 def storage_register @storage_register ||= {} end |
Instance Method Details
#save ⇒ Object
12 13 14 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 12 def save self.class.save_sub_key(sub_key, data) end |
#update_and_save(attributes) ⇒ Object
16 17 18 19 |
# File 'lib/foreman_maintain/yaml_storage.rb', line 16 def update_and_save(attributes) @data.merge!(attributes) save end |