Class: ConfigTask

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

Overview

Copyright © 2013-2015 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Method Summary collapse

Constructor Details

#initialize(config = Machinery::Config.new) ⇒ ConfigTask

Returns a new instance of ConfigTask.



19
20
21
# File 'lib/config_task.rb', line 19

def initialize(config = Machinery::Config.new)
  @config = config
end

Instance Method Details

#config(key = nil, value_string = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/config_task.rb', line 23

def config(key = nil, value_string = nil)
  if !key
    # show all config entries
    @config.each do |key, value|
      Machinery::Ui.puts "#{key} = #{value[:value]} (#{value[:description]})"
    end
  elsif !value_string
    # show one specific config entry
    Machinery::Ui.puts "#{key} = #{@config.get(key)}"
  else
    # set one specific config entry
    @config.set(key, parse_value_string(key, value_string))
    Machinery::Ui.puts "#{key} = #{@config.get(key)}"
  end
end

#parse_value_string(key, value_string) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/config_task.rb', line 39

def parse_value_string(key, value_string)
  current_value = @config.get(key)

  if current_value == true || current_value == false
    if value_string == "true" || value_string == "on"
      return true
    elsif value_string == "false" || value_string == "off"
      return false
    else
      raise Machinery::Errors::MachineryError.new("The value '#{value_string}' is not valid for key '#{key}'.")
    end
  elsif current_value.kind_of?(Integer)
    return value_string.to_i
  else
    return value_string
  end
end