Class: Worklog::Configuration
- Inherits:
-
Object
- Object
- Worklog::Configuration
- Defined in:
- lib/configuration.rb
Overview
Configuration class for the application.
Defined Under Namespace
Classes: GithubConfig, ProjectConfig
Constant Summary collapse
- CONFIGURATION_TEMPLATE =
ERB.new <<~YAML # Worklog Configuration File # This file contains configuration settings for Worklog. # You can modify the values below to customize your setup. # For more information, refer to the documentation. storage_path: <%= Dir.home %>/.worklog log_level: info timezone: 'America/Los_Angeles' webserver_port: 3000 project: # Number of last projects to show in the project list show_last: 3 github: # Your GitHub API key for accessing the GitHub API api_key: "" # Your GitHub username for finding assigned issues and PRs username: "" YAML
Instance Attribute Summary collapse
-
#github ⇒ Configuration::GithubConfig
Github related configuration.
-
#log_level ⇒ Symbol
Possible values: :debug, :info, :warn, :error, :fatal.
-
#project ⇒ Configuration::ProjectConfig
Project related configuration.
-
#storage_path ⇒ String
The path where the application stores its data.
-
#timezone ⇒ Object
Returns the value of attribute timezone.
-
#webserver_port ⇒ Integer
Default is 3000.
Class Method Summary collapse
-
.config_file_path ⇒ String
Get the default configuration file path.
-
.load ⇒ Configuration
Load configuration from a YAML file in the user’s home directory.
Instance Method Summary collapse
-
#default_storage_path? ⇒ Boolean
Check if the storage path is the default path.
-
#initialize(&block) ⇒ Configuration
constructor
Initialize configuration with optional block for setting attributes.
-
#storage_path_exist? ⇒ Boolean
Check if the storage path exists.
Constructor Details
#initialize(&block) ⇒ Configuration
Initialize configuration with optional block for setting attributes. If no block is given, default values are used.
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/configuration.rb', line 105 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
#github ⇒ Configuration::GithubConfig
Returns Github related configuration.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/configuration.rb', line 35 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 CONFIGURATION_TEMPLATE = ERB.new <<~YAML # Worklog Configuration File # This file contains configuration settings for Worklog. # You can modify the values below to customize your setup. # For more information, refer to the documentation. storage_path: <%= Dir.home %>/.worklog log_level: info timezone: 'America/Los_Angeles' webserver_port: 3000 project: # Number of last projects to show in the project list show_last: 3 github: # Your GitHub API key for accessing the GitHub API api_key: "" # Your GitHub username for finding assigned issues and PRs username: "" YAML # 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 = config_file_path 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 # Get the default configuration file path. # @return [String] the default configuration file path def self.config_file_path File.join(Dir.home, '.worklog.yaml') end end |
#log_level ⇒ Symbol
Possible values: :debug, :info, :warn, :error, :fatal
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/configuration.rb', line 35 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 CONFIGURATION_TEMPLATE = ERB.new <<~YAML # Worklog Configuration File # This file contains configuration settings for Worklog. # You can modify the values below to customize your setup. # For more information, refer to the documentation. storage_path: <%= Dir.home %>/.worklog log_level: info timezone: 'America/Los_Angeles' webserver_port: 3000 project: # Number of last projects to show in the project list show_last: 3 github: # Your GitHub API key for accessing the GitHub API api_key: "" # Your GitHub username for finding assigned issues and PRs username: "" YAML # 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 = config_file_path 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 # Get the default configuration file path. # @return [String] the default configuration file path def self.config_file_path File.join(Dir.home, '.worklog.yaml') end end |
#project ⇒ Configuration::ProjectConfig
Returns Project related configuration.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/configuration.rb', line 35 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 CONFIGURATION_TEMPLATE = ERB.new <<~YAML # Worklog Configuration File # This file contains configuration settings for Worklog. # You can modify the values below to customize your setup. # For more information, refer to the documentation. storage_path: <%= Dir.home %>/.worklog log_level: info timezone: 'America/Los_Angeles' webserver_port: 3000 project: # Number of last projects to show in the project list show_last: 3 github: # Your GitHub API key for accessing the GitHub API api_key: "" # Your GitHub username for finding assigned issues and PRs username: "" YAML # 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 = config_file_path 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 # Get the default configuration file path. # @return [String] the default configuration file path def self.config_file_path File.join(Dir.home, '.worklog.yaml') end end |
#storage_path ⇒ String
Returns The path where the application stores its data.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/configuration.rb', line 35 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 CONFIGURATION_TEMPLATE = ERB.new <<~YAML # Worklog Configuration File # This file contains configuration settings for Worklog. # You can modify the values below to customize your setup. # For more information, refer to the documentation. storage_path: <%= Dir.home %>/.worklog log_level: info timezone: 'America/Los_Angeles' webserver_port: 3000 project: # Number of last projects to show in the project list show_last: 3 github: # Your GitHub API key for accessing the GitHub API api_key: "" # Your GitHub username for finding assigned issues and PRs username: "" YAML # 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 = config_file_path 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 # Get the default configuration file path. # @return [String] the default configuration file path def self.config_file_path File.join(Dir.home, '.worklog.yaml') end end |
#timezone ⇒ Object
Returns the value of attribute timezone.
36 37 38 |
# File 'lib/configuration.rb', line 36 def timezone @timezone end |
#webserver_port ⇒ Integer
Default is 3000.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/configuration.rb', line 35 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 CONFIGURATION_TEMPLATE = ERB.new <<~YAML # Worklog Configuration File # This file contains configuration settings for Worklog. # You can modify the values below to customize your setup. # For more information, refer to the documentation. storage_path: <%= Dir.home %>/.worklog log_level: info timezone: 'America/Los_Angeles' webserver_port: 3000 project: # Number of last projects to show in the project list show_last: 3 github: # Your GitHub API key for accessing the GitHub API api_key: "" # Your GitHub username for finding assigned issues and PRs username: "" YAML # 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 = config_file_path 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 # Get the default configuration file path. # @return [String] the default configuration file path def self.config_file_path File.join(Dir.home, '.worklog.yaml') end end |
Class Method Details
.config_file_path ⇒ String
Get the default configuration file path.
154 155 156 |
# File 'lib/configuration.rb', line 154 def self.config_file_path File.join(Dir.home, '.worklog.yaml') end |
.load ⇒ Configuration
Load configuration from a YAML file in the user’s home directory. If the file does not exist, it will use default values.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/configuration.rb', line 122 def self.load file_path = config_file_path 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.
148 149 150 |
# File 'lib/configuration.rb', line 148 def default_storage_path? @storage_path == File.join(Dir.home, '.worklog') end |
#storage_path_exist? ⇒ Boolean
Check if the storage path exists.
142 143 144 |
# File 'lib/configuration.rb', line 142 def storage_path_exist? File.exist?(@storage_path) end |