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
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
-
.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.
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
#github ⇒ Configuration::GithubConfig
Returns Github related configuration.
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_level ⇒ Symbol
Possible values: :debug, :info, :warn, :error, :fatal
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 |
#project ⇒ Configuration::ProjectConfig
Returns Project related configuration.
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_path ⇒ String
Returns 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 |
#timezone ⇒ Object
Returns the value of attribute timezone.
35 36 37 |
# File 'lib/configuration.rb', line 35 def timezone @timezone end |
#webserver_port ⇒ Integer
Default is 3000.
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
.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.
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.
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.
120 121 122 |
# File 'lib/configuration.rb', line 120 def storage_path_exist? File.exist?(@storage_path) end |