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.



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

#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



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_fileObject



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_allObject



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_pathObject



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

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

.storage_registerObject



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

def storage_register
  @storage_register ||= {}
end

Instance Method Details

#saveObject



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