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.



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

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
                    TomlRB.load_file config_file_path
                  rescue Exception => e
                    raise TomlParseFailure, "could not parse TOML file '#{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.



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

def config_file_path
  @config_file_path
end

Instance Method Details

#persist!Object



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

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

  content = TomlRB.dump(user_config)

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

#profile_nameObject



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

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