Class: ForemanMaintain::YamlStorage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/foreman_maintain/yaml_storage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/foreman_maintain/yaml_storage.rb', line 4

def data
  @data
end

#sub_keyObject (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_fileObject



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_allObject



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_pathObject



31
32
33
# File 'lib/foreman_maintain/yaml_storage.rb', line 31

def storage_file_path
  File.expand_path(ForemanMaintain.config.storage_file)
end

.storage_registerObject



35
36
37
# File 'lib/foreman_maintain/yaml_storage.rb', line 35

def storage_register
  @storage_register ||= {}
end

Instance Method Details

#saveObject



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