Class: Raykit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/configuration.rb

Constant Summary collapse

CONFIG_DIR =

Define a subdirectory and filename for the config file in a cross-platform manner.

File.join(Dir.home, ".config", "raykit")
CONFIG_FILE =
File.join(CONFIG_DIR, "config.json")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



11
12
13
14
15
16
17
18
19
20
# File 'lib/raykit/configuration.rb', line 11

def initialize
  if File.exist?(CONFIG_FILE)
    load_configuration
  else
    set_default_configuration
    if (!Dir.exist?(CONFIG_DIR))
      save_configuration # Save the default configuration if no configuration file exists.
    end
  end
end

Instance Attribute Details

#auto_setupObject

Returns the value of attribute auto_setup.



5
6
7
# File 'lib/raykit/configuration.rb', line 5

def auto_setup
  @auto_setup
end

#backup_dirObject

Returns the value of attribute backup_dir.



5
6
7
# File 'lib/raykit/configuration.rb', line 5

def backup_dir
  @backup_dir
end

#log_levelObject

Returns the value of attribute log_level.



5
6
7
# File 'lib/raykit/configuration.rb', line 5

def log_level
  @log_level
end

#root_dirObject

Returns the value of attribute root_dir.



5
6
7
# File 'lib/raykit/configuration.rb', line 5

def root_dir
  @root_dir
end

Instance Method Details

#load_configurationObject



22
23
24
25
26
27
28
# File 'lib/raykit/configuration.rb', line 22

def load_configuration
  config_data = JSON.parse(File.read(CONFIG_FILE))
  @root_dir = config_data["root_dir"]
  @auto_setup = config_data["auto_setup"]
  @backup_dir = config_data["backup_dir"]
  @log_level = config_data["log_level"]
end

#save_configurationObject



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

def save_configuration
  # Create the config directory if it doesn't exist.
  FileUtils.mkdir_p(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)

  File.write(CONFIG_FILE, {
    root_dir: @root_dir,
    auto_setup: @auto_setup,
    backup_dir: @backup_dir,
    log_level: @log_level,
  }.to_json)
end

#set_default_configurationObject



30
31
32
33
34
35
# File 'lib/raykit/configuration.rb', line 30

def set_default_configuration
  @root_dir = ""
  @auto_setup = false
  @backup_dir = "backup"
  @log_level = ::Logger::DEBUG
end

#to_sObject



49
50
51
# File 'lib/raykit/configuration.rb', line 49

def to_s
  "Root Directory: #{@root_dir}\nAuto Setup: #{@auto_setup}\nBackup Directory: #{@backup_dir}"
end