Class: SlackPomodoroTimer::Config
- Inherits:
-
Object
- Object
- SlackPomodoroTimer::Config
- Defined in:
- lib/slack_pomodoro_timer/config.rb
Constant Summary collapse
- FILENAME =
'.slack_pomodoro_timer'- PATH =
"#{Dir.home}/#{FILENAME}"- @@config =
{}
Class Method Summary collapse
-
.add(options = {}) ⇒ Object
Add a config option or options.
-
.add_option(key, value) ⇒ Object
Sets the key to the given value unless it is nil.
-
.config ⇒ Object
Class variable getter returns a hash of config values.
-
.configured? ⇒ Boolean
Returns true if configured properly.
-
.defaults ⇒ Object
Returns the default config values.
-
.display ⇒ Object
Display all config values.
-
.get(key) ⇒ Object
Get a config value by key.
-
.load ⇒ Object
Loads the config file if it exists or sets default values if the file does not exist.
-
.save ⇒ Object
Saves the current config hash as YAML to the path.
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(={}) add_option(:channel, [:channel]) add_option(:url, [: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 |
.config ⇒ Object
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
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 |
.defaults ⇒ Object
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 |
.display ⇒ Object
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 |
.load ⇒ Object
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 |
.save ⇒ Object
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 |