Class: CalendarAssistant::CLI::Config

Inherits:
CalendarAssistant::Config show all
Defined in:
lib/calendar_assistant/cli/config.rb

Defined Under Namespace

Classes: NoConfigFileToPersist, TomlParseFailure

Constant Summary collapse

CONFIG_FILE_PATH =
File.join (ENV['CA_HOME'] || ENV["HOME"]), ".calendar-assistant"

Constants inherited from CalendarAssistant::Config

CalendarAssistant::Config::DEFAULT_CALENDAR_ID, CalendarAssistant::Config::DEFAULT_SETTINGS

Instance Attribute Summary collapse

Attributes inherited from CalendarAssistant::Config

#defaults, #options, #user_config

Instance Method Summary collapse

Methods inherited from CalendarAssistant::Config

#calendar_ids, #debug?, #event_visibility, #get, #in_env, #must_be, #must_not_be, #set, #setting, #settings, #token_store, #tokens

Constructor Details

#initialize(options: {}, config_file_path: CONFIG_FILE_PATH, defaults: DEFAULT_SETTINGS) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/calendar_assistant/cli/config.rb', line 12

def initialize options: {},
               config_file_path: CONFIG_FILE_PATH,
               defaults: DEFAULT_SETTINGS


  @config_file_path = config_file_path

  user_config = if File.exist? config_file_path
                   begin
                     FileUtils.chmod 0600, config_file_path
                     TOML.load_file config_file_path
                   rescue Exception => e
                     raise TomlParseFailure, "could not parse #{config_file_path}: #{e}"
                   end
                 else
                   Hash.new
                 end
  super(options: options, defaults: defaults, user_config: user_config)
end

Instance Attribute Details

#config_file_pathObject (readonly)

Returns the value of attribute config_file_path.



10
11
12
# File 'lib/calendar_assistant/cli/config.rb', line 10

def config_file_path
  @config_file_path
end

Instance Method Details

#persist!Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/calendar_assistant/cli/config.rb', line 38

def persist!
  if config_file_path.nil?
    raise NoConfigFileToPersist, "Cannot persist config when there's no config file"
  end

  content = TOML::Generator.new(user_config).body

  File.open(config_file_path, "w") do |f|
    f.write content
  end
end

#profile_nameObject



32
33
34
35
36
# File 'lib/calendar_assistant/cli/config.rb', line 32

def profile_name
  super.tap do |token|
    persist!
  end
end