Class: CloudDoor::CloudYaml

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_door/cloud_yaml.rb

Direct Known Subclasses

Account, Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/cloud_door/cloud_yaml.rb', line 5

def file
  @file
end

#itemsObject

Returns the value of attribute items.



5
6
7
# File 'lib/cloud_door/cloud_yaml.rb', line 5

def items
  @items
end

#storageObject

Returns the value of attribute storage.



5
6
7
# File 'lib/cloud_door/cloud_yaml.rb', line 5

def storage
  @storage
end

Instance Method Details

#load_yamlObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cloud_door/cloud_yaml.rb', line 7

def load_yaml
  return false unless File.exist?(@file)
  all_config = YAML.load_file(@file)
  return false unless all_config.is_a?(Hash)
  return false unless all_config.key?(@storage)
  config = all_config[@storage]
  @items.each do |item|
    instance_variable_set("@#{item}", config[item]) if config.key?(item)
  end
  true
end

#update_yaml(update_params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloud_door/cloud_yaml.rb', line 19

def update_yaml(update_params)
  if File.exist?(@file)
    all_config = YAML.load_file(@file)
    if all_config.key?(@storage)
      config = all_config[@storage]
    else
      config = {}
    end
  else
    all_config = {}
    config     = {}
  end
  @items.each do |item|
    next unless update_params.key?(item)
    config[item] = update_params[item]
  end
  begin
    all_config[@storage] = config
    open(@file, 'wb') { |f| YAML.dump(all_config, f) }
    load_yaml
    true
  rescue
    false
  end
end