Class: Worklog::Configuration
- Inherits:
-
Object
- Object
- Worklog::Configuration
- Defined in:
- lib/configuration.rb
Overview
Configuration class for the application.
Instance Attribute Summary collapse
-
#log_level ⇒ Symbol
Possible values: :debug, :info, :warn, :error, :fatal.
-
#storage_path ⇒ String
The path where the application stores its data.
-
#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
A new instance of Configuration.
-
#storage_path_exist? ⇒ Boolean
Check if the storage path exists.
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_level ⇒ Symbol
Possible values: :debug, :info, :warn, :error, :fatal
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_path ⇒ String
Returns 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_port ⇒ Integer
Default is 3000.
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
.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.
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.
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.
49 50 51 |
# File 'lib/configuration.rb', line 49 def storage_path_exist? File.exist?(@storage_path) end |