Class: HammerCLI::Defaults

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

Defined Under Namespace

Classes: DefaultsError, DefaultsPathError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, file_path = nil) ⇒ Defaults

Returns a new instance of Defaults.



11
12
13
14
# File 'lib/hammer_cli/defaults.rb', line 11

def initialize(settings, file_path = nil)
  @defaults_settings = settings || {}
  @path = file_path || DEFAULT_FILE
end

Instance Attribute Details

#defaults_settingsObject (readonly)

Returns the value of attribute defaults_settings.



9
10
11
# File 'lib/hammer_cli/defaults.rb', line 9

def defaults_settings
  @defaults_settings
end

Instance Method Details

#add_defaults_to_conf(default_options, provider) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/hammer_cli/defaults.rb', line 29

def add_defaults_to_conf(default_options, provider)
  create_default_file if defaults_settings.empty?
  default_options.each do |key, value|
    key = switch_to_name(key).to_sym
    @defaults_settings.delete_if { |k,| defaults_match?(k, key) }
    @defaults_settings[key] = (value ? {:value => value.to_s} : {:provider => provider.to_s})
  end
  update_defaults_file
end

#defaults_set?(param) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/hammer_cli/defaults.rb', line 39

def defaults_set?(param)
  defaults_settings.keys.any? { |k| defaults_match?(k, param) }
end

#delete_default_from_conf(param) ⇒ Object



24
25
26
27
# File 'lib/hammer_cli/defaults.rb', line 24

def delete_default_from_conf(param)
  @defaults_settings.delete_if { |k,| defaults_match?(k, param) }
  update_defaults_file
end

#get_defaults(opt) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hammer_cli/defaults.rb', line 43

def get_defaults(opt)
  unless defaults_settings.nil?
    option_key = normalize_option(opt)
    settings_key = defaults_settings[option_key.to_sym].nil? ? option_key.gsub('_','-').to_sym : option_key.to_sym

    return nil if defaults_settings[settings_key].nil?

    if defaults_settings[settings_key][:provider]
      providers[defaults_settings[settings_key][:provider]].get_defaults(option_key.to_sym)
    else
      defaults_settings[settings_key][:value]
    end
  end
end

#providersObject



20
21
22
# File 'lib/hammer_cli/defaults.rb', line 20

def providers
  @providers ||= {}
end

#register_provider(provider) ⇒ Object



16
17
18
# File 'lib/hammer_cli/defaults.rb', line 16

def register_provider(provider)
  providers[provider.provider_name.to_s] = provider
end

#write_to_file(defaults) ⇒ Object



58
59
60
61
62
# File 'lib/hammer_cli/defaults.rb', line 58

def write_to_file(defaults)
  File.open(path,'w') do |h|
    h.write defaults.to_yaml
  end
end