Class: AssistedWorkflow::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/assisted_workflow/config_file.rb

Overview

class providing methods to load, manage and save configuration files

Instance Method Summary collapse

Constructor Details

#initialize(awfile) ⇒ ConfigFile

Returns a new instance of ConfigFile.



51
52
53
54
55
56
57
58
# File 'lib/assisted_workflow/config_file.rb', line 51

def initialize(awfile)
  @awfile = awfile
  @hash = if File.exists?(@awfile)
    ConfigHash.new(::YAML::load_file(@awfile) || {})
  else
    ConfigHash.new
  end
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
# File 'lib/assisted_workflow/config_file.rb', line 36

def [](key)
  @hash[key]
end

#merge_file(file) ⇒ Object

merges other config file into the current one



45
46
47
48
49
# File 'lib/assisted_workflow/config_file.rb', line 45

def merge_file(file)
  other_config = ConfigFile.new(file)
  @hash.deep_merge!(other_config.to_hash)
  self
end

#parse(args) ⇒ Object

parse a command line arguments into configuration keys Example:

pivotal.token=mypivotaltoken
=> {:pivotal => {:token => "mypivotaltoken"}}


18
19
20
21
22
23
24
25
26
27
# File 'lib/assisted_workflow/config_file.rb', line 18

def parse(args)
  Array(args).each do |values|
    keys, value = values.split("=")
    keys.split(".").reverse.each do |k|
      value = {k => value}
    end
    @hash.deep_merge!(value)
  end
  self
end

#save!Object

dumps the configuration values to a file in yaml format



30
31
32
33
34
# File 'lib/assisted_workflow/config_file.rb', line 30

def save!
  content = @hash.to_yaml
  content.gsub! " !ruby/hash:AssistedWorkflow::ConfigHash", ""
  File.open(@awfile, 'w'){ |f| f.write(content) }
end

#to_hashObject



40
41
42
# File 'lib/assisted_workflow/config_file.rb', line 40

def to_hash
  @hash.dup
end