Class: TerminalCal::Config

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

Constant Summary collapse

APP_DIR =
File.join(Dir.home, '.terminal_cal')
CONFIG_FILE =
'config.yaml'.freeze
CONFIG_PATH =
File.join(APP_DIR, CONFIG_FILE)

Class Method Summary collapse

Class Method Details

.calendarsObject



34
35
36
# File 'lib/terminal_cal/config.rb', line 34

def self.calendars
  read[:calendars] || []
end

.clobberObject



38
39
40
# File 'lib/terminal_cal/config.rb', line 38

def self.clobber
  File.delete(CONFIG_PATH)
end

.config_templateObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/terminal_cal/config.rb', line 42

def self.config_template
  { calendars: [
      { name: 'Some Calendar',
        source: 'Some URL or file path',
        cache: true,
        cache_life_minutes: 30
      }
    ]
  }.to_yaml
end

.loadObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/terminal_cal/config.rb', line 7

def self.load
  unless File.exist?(CONFIG_PATH)
    FileUtils.mkdir_p(APP_DIR)
    File.write(CONFIG_PATH, config_template)
    FileUtils.touch(CONFIG_PATH)

    msg = <<~MSG
      New config generated at #{CONFIG_PATH}
      Please update config and then try again.
    MSG

    raise TerminalCal::Errors::AppExit, msg
  end

  self.read
end

.readObject



24
25
26
# File 'lib/terminal_cal/config.rb', line 24

def self.read
  YAML.load_file(CONFIG_PATH) || {}
end

.write(key, value) ⇒ Object



28
29
30
31
32
# File 'lib/terminal_cal/config.rb', line 28

def self.write(key, value)
  config = self.read
  config[key] = value
  File.write(CONFIG_PATH, config.to_yaml)
end