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.



19
20
21
22
23
24
25
26
27
# File 'lib/configuration.rb', line 19

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_levelSymbol

Possible values: :debug, :info, :warn, :error, :fatal

Returns:

  • (Symbol)

    The logging level for the application.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/configuration.rb', line 16

class Configuration
  attr_accessor :storage_path, :log_level, :webserver_port

  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

  # Load configuration from a YAML file in the user's home directory.
  # If the file does not exist, it will use default values.
  # @return [Configuration] the loaded configuration
  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

  # Check if the storage path exists.
  # @return [Boolean] true if the storage path exists, false otherwise
  def storage_path_exist?
    File.exist?(@storage_path)
  end

  # Check if the storage path is the default path.
  # @return [Boolean] true if the storage path is the default, false otherwise
  def default_storage_path?
    @storage_path == File.join(Dir.home, '.worklog')
  end
end

#storage_pathString

Returns The path where the application stores its data.

Returns:

  • (String)

    The path where the application stores its data.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/configuration.rb', line 16

class Configuration
  attr_accessor :storage_path, :log_level, :webserver_port

  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

  # Load configuration from a YAML file in the user's home directory.
  # If the file does not exist, it will use default values.
  # @return [Configuration] the loaded configuration
  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

  # Check if the storage path exists.
  # @return [Boolean] true if the storage path exists, false otherwise
  def storage_path_exist?
    File.exist?(@storage_path)
  end

  # Check if the storage path is the default path.
  # @return [Boolean] true if the storage path is the default, false otherwise
  def default_storage_path?
    @storage_path == File.join(Dir.home, '.worklog')
  end
end

#webserver_portInteger

Default is 3000.

Returns:

  • (Integer)

    The port on which the web server runs.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/configuration.rb', line 16

class Configuration
  attr_accessor :storage_path, :log_level, :webserver_port

  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

  # Load configuration from a YAML file in the user's home directory.
  # If the file does not exist, it will use default values.
  # @return [Configuration] the loaded configuration
  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

  # Check if the storage path exists.
  # @return [Boolean] true if the storage path exists, false otherwise
  def storage_path_exist?
    File.exist?(@storage_path)
  end

  # Check if the storage path is the default path.
  # @return [Boolean] true if the storage path is the default, false otherwise
  def default_storage_path?
    @storage_path == File.join(Dir.home, '.worklog')
  end
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:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/configuration.rb', line 32

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

Instance Method Details

#default_storage_path?Boolean

Check if the storage path is the default path.

Returns:

  • (Boolean)

    true if the storage path is the default, false otherwise



55
56
57
# File 'lib/configuration.rb', line 55

def default_storage_path?
  @storage_path == File.join(Dir.home, '.worklog')
end

#storage_path_exist?Boolean

Check if the storage path exists.

Returns:

  • (Boolean)

    true if the storage path exists, false otherwise



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

def storage_path_exist?
  File.exist?(@storage_path)
end