Class: SlackPomodoroTimer::Config

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

Constant Summary collapse

FILENAME =
'.slack_pomodoro_timer'
PATH =
"#{Dir.home}/#{FILENAME}"
@@config =
{}

Class Method Summary collapse

Class Method Details

.add(options = {}) ⇒ Object

Add a config option or options



35
36
37
38
# File 'lib/slack_pomodoro_timer/config.rb', line 35

def self.add(options={})
  add_option(:channel, options[:channel])
  add_option(:url, options[:url])
end

.add_option(key, value) ⇒ Object

Sets the key to the given value unless it is nil



98
99
100
# File 'lib/slack_pomodoro_timer/config.rb', line 98

def self.add_option(key, value)
  config[key] = value unless value.nil?
end

.configObject

Class variable getter returns a hash of config values



15
16
17
# File 'lib/slack_pomodoro_timer/config.rb', line 15

def self.config
  @@config
end

.configured?Boolean

Returns true if configured properly

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/slack_pomodoro_timer/config.rb', line 22

def self.configured?
  self.load
  if @@config.empty? ||
     @@config.any? { |key, value| value.nil? || value.empty? }
    false
  else
    true
  end
end

.defaultsObject

Returns the default config values



88
89
90
91
92
93
# File 'lib/slack_pomodoro_timer/config.rb', line 88

def self.defaults
  {
    :channel => 'general',
    :url => '',
  }
end

.displayObject

Display all config values



72
73
74
75
76
77
78
79
80
# File 'lib/slack_pomodoro_timer/config.rb', line 72

def self.display
  if config.empty?
    puts "No config values set"
  else
    config.each do |key, value|
      puts "#{key.upcase}: #{value}"
    end
  end
end

.get(key) ⇒ Object

Get a config value by key



66
67
68
# File 'lib/slack_pomodoro_timer/config.rb', line 66

def self.get(key)
  config[key]
end

.loadObject

Loads the config file if it exists or sets default values if the file does not exist



54
55
56
57
58
59
60
61
62
# File 'lib/slack_pomodoro_timer/config.rb', line 54

def self.load
  file_exists = File.exists?(PATH)
  if File.exists?(PATH)
    value = YAML.load_file(PATH)
  else
    value = defaults
  end
  @@config = value
end

.saveObject

Saves the current config hash as YAML to the path



44
45
46
47
48
# File 'lib/slack_pomodoro_timer/config.rb', line 44

def self.save
  File.open(PATH, 'w+') do |f|
    f.write(YAML.dump(config))
  end
end