Class: Worklog::Configuration

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

Overview

Configuration class for the application.

Examples:

Example ~/.worklog.yaml

storage_path: /Users/username/.worklog
log_level: debug
timezone: 'America/Los_Angeles'
webserver_port: 4000

project:
  show_last: 3

github:
  api_key: 123abc
  username: sample-user

Defined Under Namespace

Classes: GithubConfig, ProjectConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Configuration

Initialize configuration with optional block for setting attributes. If no block is given, default values are used.

Examples:

Configuration.new do |config|
  config.storage_path = '/custom/path'
  config.log_level = :debug
  config.timezone = 'America/Los_Angeles'
end


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/configuration.rb', line 83

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)
  @timezone ||= 'America/Los_Angeles'
  @timezone = TZInfo::Timezone.get(@timezone) if @timezone.is_a?(String)
  @webserver_port ||= 3000
  @project = ProjectConfig.new
  @github ||= GithubConfig.new
end

Instance Attribute Details

#githubConfiguration::GithubConfig

Returns Github related configuration.

Returns:



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/configuration.rb', line 34

class Configuration
  attr_accessor :storage_path, :log_level, :timezone, :webserver_port, :project, :github

  # Configuration for projects
  # @!attribute [rw] show_last
  #   @return [Integer] Number of last projects to show in the project list.
  class ProjectConfig
    attr_accessor :show_last

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   ProjectConfig.new({'show_last' => 5})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Configuration for Github API access.
  # @!attribute [rw] api_key
  #   @return [String] The API key for Github access.
  # @!attribute [rw] username
  #   @return [String] The Github username.
  class GithubConfig
    attr_accessor :api_key, :username

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   GithubConfig.new({'api_key' => '123abc', 'username' => 'sample-user'})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Initialize configuration with optional block for setting attributes.
  # If no block is given, default values are used.
  # @example
  #   Configuration.new do |config|
  #     config.storage_path = '/custom/path'
  #     config.log_level = :debug
  #     config.timezone = 'America/Los_Angeles'
  #   end
  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)
    @timezone ||= 'America/Los_Angeles'
    @timezone = TZInfo::Timezone.get(@timezone) if @timezone.is_a?(String)
    @webserver_port ||= 3000
    @project = ProjectConfig.new
    @github ||= GithubConfig.new
  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']

      config.project = ProjectConfig.new(file_cfg['project'])
      config.github = GithubConfig.new(file_cfg['github'])
    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

#log_levelSymbol

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

Returns:

  • (Symbol)

    The logging level for the application.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/configuration.rb', line 34

class Configuration
  attr_accessor :storage_path, :log_level, :timezone, :webserver_port, :project, :github

  # Configuration for projects
  # @!attribute [rw] show_last
  #   @return [Integer] Number of last projects to show in the project list.
  class ProjectConfig
    attr_accessor :show_last

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   ProjectConfig.new({'show_last' => 5})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Configuration for Github API access.
  # @!attribute [rw] api_key
  #   @return [String] The API key for Github access.
  # @!attribute [rw] username
  #   @return [String] The Github username.
  class GithubConfig
    attr_accessor :api_key, :username

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   GithubConfig.new({'api_key' => '123abc', 'username' => 'sample-user'})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Initialize configuration with optional block for setting attributes.
  # If no block is given, default values are used.
  # @example
  #   Configuration.new do |config|
  #     config.storage_path = '/custom/path'
  #     config.log_level = :debug
  #     config.timezone = 'America/Los_Angeles'
  #   end
  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)
    @timezone ||= 'America/Los_Angeles'
    @timezone = TZInfo::Timezone.get(@timezone) if @timezone.is_a?(String)
    @webserver_port ||= 3000
    @project = ProjectConfig.new
    @github ||= GithubConfig.new
  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']

      config.project = ProjectConfig.new(file_cfg['project'])
      config.github = GithubConfig.new(file_cfg['github'])
    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

#projectConfiguration::ProjectConfig

Returns Project related configuration.

Returns:



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/configuration.rb', line 34

class Configuration
  attr_accessor :storage_path, :log_level, :timezone, :webserver_port, :project, :github

  # Configuration for projects
  # @!attribute [rw] show_last
  #   @return [Integer] Number of last projects to show in the project list.
  class ProjectConfig
    attr_accessor :show_last

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   ProjectConfig.new({'show_last' => 5})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Configuration for Github API access.
  # @!attribute [rw] api_key
  #   @return [String] The API key for Github access.
  # @!attribute [rw] username
  #   @return [String] The Github username.
  class GithubConfig
    attr_accessor :api_key, :username

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   GithubConfig.new({'api_key' => '123abc', 'username' => 'sample-user'})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Initialize configuration with optional block for setting attributes.
  # If no block is given, default values are used.
  # @example
  #   Configuration.new do |config|
  #     config.storage_path = '/custom/path'
  #     config.log_level = :debug
  #     config.timezone = 'America/Los_Angeles'
  #   end
  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)
    @timezone ||= 'America/Los_Angeles'
    @timezone = TZInfo::Timezone.get(@timezone) if @timezone.is_a?(String)
    @webserver_port ||= 3000
    @project = ProjectConfig.new
    @github ||= GithubConfig.new
  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']

      config.project = ProjectConfig.new(file_cfg['project'])
      config.github = GithubConfig.new(file_cfg['github'])
    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.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/configuration.rb', line 34

class Configuration
  attr_accessor :storage_path, :log_level, :timezone, :webserver_port, :project, :github

  # Configuration for projects
  # @!attribute [rw] show_last
  #   @return [Integer] Number of last projects to show in the project list.
  class ProjectConfig
    attr_accessor :show_last

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   ProjectConfig.new({'show_last' => 5})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Configuration for Github API access.
  # @!attribute [rw] api_key
  #   @return [String] The API key for Github access.
  # @!attribute [rw] username
  #   @return [String] The Github username.
  class GithubConfig
    attr_accessor :api_key, :username

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   GithubConfig.new({'api_key' => '123abc', 'username' => 'sample-user'})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Initialize configuration with optional block for setting attributes.
  # If no block is given, default values are used.
  # @example
  #   Configuration.new do |config|
  #     config.storage_path = '/custom/path'
  #     config.log_level = :debug
  #     config.timezone = 'America/Los_Angeles'
  #   end
  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)
    @timezone ||= 'America/Los_Angeles'
    @timezone = TZInfo::Timezone.get(@timezone) if @timezone.is_a?(String)
    @webserver_port ||= 3000
    @project = ProjectConfig.new
    @github ||= GithubConfig.new
  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']

      config.project = ProjectConfig.new(file_cfg['project'])
      config.github = GithubConfig.new(file_cfg['github'])
    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

#timezoneObject

Returns the value of attribute timezone.



35
36
37
# File 'lib/configuration.rb', line 35

def timezone
  @timezone
end

#webserver_portInteger

Default is 3000.

Returns:

  • (Integer)

    The port on which the web server runs.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/configuration.rb', line 34

class Configuration
  attr_accessor :storage_path, :log_level, :timezone, :webserver_port, :project, :github

  # Configuration for projects
  # @!attribute [rw] show_last
  #   @return [Integer] Number of last projects to show in the project list.
  class ProjectConfig
    attr_accessor :show_last

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   ProjectConfig.new({'show_last' => 5})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Configuration for Github API access.
  # @!attribute [rw] api_key
  #   @return [String] The API key for Github access.
  # @!attribute [rw] username
  #   @return [String] The Github username.
  class GithubConfig
    attr_accessor :api_key, :username

    # Initialize with default values, parameters can be overridden via hash
    # @example
    #   GithubConfig.new({'api_key' => '123abc', 'username' => 'sample-user'})
    def initialize(params = {})
      return if params.nil?

      params.each do |key, value|
        instance_variable_set("@#{key}", value) if respond_to?("#{key}=")
      end
    end
  end

  # Initialize configuration with optional block for setting attributes.
  # If no block is given, default values are used.
  # @example
  #   Configuration.new do |config|
  #     config.storage_path = '/custom/path'
  #     config.log_level = :debug
  #     config.timezone = 'America/Los_Angeles'
  #   end
  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)
    @timezone ||= 'America/Los_Angeles'
    @timezone = TZInfo::Timezone.get(@timezone) if @timezone.is_a?(String)
    @webserver_port ||= 3000
    @project = ProjectConfig.new
    @github ||= GithubConfig.new
  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']

      config.project = ProjectConfig.new(file_cfg['project'])
      config.github = GithubConfig.new(file_cfg['github'])
    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:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/configuration.rb', line 100

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']

    config.project = ProjectConfig.new(file_cfg['project'])
    config.github = GithubConfig.new(file_cfg['github'])
  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



126
127
128
# File 'lib/configuration.rb', line 126

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



120
121
122
# File 'lib/configuration.rb', line 120

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