Class: Worklog::Configuration

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

Overview

Configuration class for the application.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Configuration

Returns a new instance of Configuration.



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

def initialize(&block)
  block.call(self) if block_given?

  # Set default values if not set
  @storage_path ||= File.join(Dir.home, '.worklog')
  @log_level = log_level || :info
  @log_level = @log_level.to_sym if @log_level.is_a?(String)
  @webserver_port ||= 3000
end

Instance Attribute Details

#log_levelObject

Returns the value of attribute log_level.



9
10
11
# File 'lib/configuration.rb', line 9

def log_level
  @log_level
end

#storage_pathObject

Returns the value of attribute storage_path.



9
10
11
# File 'lib/configuration.rb', line 9

def storage_path
  @storage_path
end

#webserver_portObject

Returns the value of attribute webserver_port.



9
10
11
# File 'lib/configuration.rb', line 9

def webserver_port
  @webserver_port
end

Class Method Details

.loadConfiguration

Load configuration from a YAML file in the user’s home directory. If the file does not exist, it will use default values.

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/configuration.rb', line 24

def self.load
  file_path = File.join(Dir.home, '.worklog.yaml')
  config = Configuration.new
  if File.exist?(file_path)
    file_cfg = YAML.load_file(file_path)
    config.storage_path = file_cfg['storage_path'] if file_cfg['storage_path']
    config.log_level = file_cfg['log_level'].to_sym if file_cfg['log_level']
    config.webserver_port = file_cfg['webserver_port'] if file_cfg['webserver_port']
  else
    WorkLogger.debug "Configuration file does not exist in #{file_path}, using defaults."
  end

  config
end