Module: StickyFlag::Configuration

Included in:
ThorApp
Defined in:
lib/stickyflag/configuration.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  :have_pdftk => false,
  :pdftk_path => '',
  :have_mkvextract => false,
  :mkvextract_path => '',
  :have_mkvpropedit => false,
  :mkvpropedit_path => '',
  
  :root => ''
}

Instance Method Summary collapse

Instance Method Details

#dump_configObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/stickyflag/configuration.rb', line 44

def dump_config
  @configuration ||= DEFAULT_CONFIG.clone
  
  return if options.quiet?

  say "StickyFlag Configuration:"
  @configuration.each do |key, val|
    say "  #{key}: '#{val}'"
  end
end

#get_config(key) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/stickyflag/configuration.rb', line 19

def get_config(key)
  @configuration ||= DEFAULT_CONFIG.clone
  
  unless @configuration.keys.include?(key.to_sym)
    raise Thor::Error.new("ERROR: Invalid configuration key (#{key.to_s})") 
  end

  @configuration[key.to_sym]
end

#load_config!Object



55
56
57
58
59
60
61
62
63
# File 'lib/stickyflag/configuration.rb', line 55

def load_config!
  file_name = config_path
  if File.file? file_name
    @configuration = YAML::load(File.open(file_name, 'r:UTF-8'))
    
    # Merge with the default to pick up new keys
    @configuration = DEFAULT_CONFIG.merge(@configuration)
  end
end

#reset_config!Object



39
40
41
42
# File 'lib/stickyflag/configuration.rb', line 39

def reset_config!
  @configuration = DEFAULT_CONFIG.clone
  save_config!
end

#save_config!Object



65
66
67
68
69
70
71
72
# File 'lib/stickyflag/configuration.rb', line 65

def save_config!
  @configuration ||= DEFAULT_CONFIG.clone
  
  file_name = config_path
  File.open(file_name, 'w:UTF-8') do |f|
    YAML.dump(@configuration, f)
  end
end

#set_config(key, value) ⇒ Object



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

def set_config(key, value)
  @configuration ||= DEFAULT_CONFIG.clone
  
  unless @configuration.keys.include?(key.to_sym)
    raise Thor::Error.new("ERROR: invalid configuration key (#{key.to_s})")
  end

  @configuration[key.to_sym] = value
end