Class: Translatomatic::Config
- Inherits:
-
Object
- Object
- Translatomatic::Config
- Includes:
- Util
- Defined in:
- lib/translatomatic/config.rb
Overview
Translatomatic configuration. Configuration settings may be specified in the following locations:
- environment variables
- user configuration file $HOME/.translatomatic/config.yml
- project configuration file $PROJECT/.translatomatic/config.yml
- command line
Settings are read in the order given above, with last setting found taking precedence over values read earlier.
Instance Attribute Summary collapse
-
#default_locale ⇒ String
The default locale.
-
#logger ⇒ Logger
The logger instance.
-
#project_settings_path ⇒ String
readonly
The path to the project settings file.
-
#user_settings_path ⇒ String
readonly
The path to the user settings file.
Class Method Summary collapse
-
.options ⇒ Array<Translatomatic::Option] all available options
Array<Translatomatic::Option] all available options.
Instance Method Summary collapse
-
#get(key, context = nil) ⇒ String
Get a configuration setting.
-
#include?(key, context = nil) ⇒ String, boolean
Test if configuration includes the given key.
-
#load ⇒ Object
Load configuration from the config file(s).
-
#project_path ⇒ Object
The project path is found by searching for a ‘.translatomatic’ directory that is not within the user home directory.
-
#remove(key, context = nil) ⇒ void
Remove a configuration setting.
-
#reset ⇒ Object
Reset all configuration to the defaults.
-
#save ⇒ Object
Save configuration settings.
-
#set(key, value, context = nil) ⇒ String
Change a configuration setting.
Instance Attribute Details
#default_locale ⇒ String
Returns The default locale.
20 21 22 |
# File 'lib/translatomatic/config.rb', line 20 def default_locale @default_locale end |
#logger ⇒ Logger
Returns The logger instance.
17 18 19 |
# File 'lib/translatomatic/config.rb', line 17 def logger @logger end |
#project_settings_path ⇒ String
Returns The path to the project settings file.
26 27 28 |
# File 'lib/translatomatic/config.rb', line 26 def project_settings_path @project_settings_path end |
#user_settings_path ⇒ String
Returns The path to the user settings file.
23 24 25 |
# File 'lib/translatomatic/config.rb', line 23 def user_settings_path @user_settings_path end |
Class Method Details
.options ⇒ Array<Translatomatic::Option] all available options
Returns Array<Translatomatic::Option] all available options.
127 128 129 |
# File 'lib/translatomatic/config.rb', line 127 def self. self..values end |
Instance Method Details
#get(key, context = nil) ⇒ String
Get a configuration setting
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/translatomatic/config.rb', line 52 def get(key, context = nil) key = check_valid_key(key) option = option(key) value = option.default # set to default value if context.nil? # find the first setting in the following order CONTEXTS.each do |ctx| if @settings[ctx].include?(key) value = @settings[ctx][key] break end end else # context is set context = check_valid_context(context) if @settings[context].include?(key) value = @settings[context][key] end end # cast value to expected type cast(value, option.type) end |
#include?(key, context = nil) ⇒ String, boolean
Test if configuration includes the given key
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/translatomatic/config.rb', line 94 def include?(key, context = nil) key = check_valid_key(key) if context.nil? CONTEXTS.each do |ctx| return true if @settings[ctx].include?(key) end false else context = check_valid_context(context) @settings[context].include?(key) end end |
#load ⇒ Object
Load configuration from the config file(s)
114 115 116 117 118 |
# File 'lib/translatomatic/config.rb', line 114 def load load_context_env load_context(:user, @user_settings_path) load_context(:project, @project_settings_path) end |
#project_path ⇒ Object
The project path is found by searching for a ‘.translatomatic’ directory that is not within the user home directory. The search ascends upwards from the current working directory.
136 137 138 139 140 141 142 |
# File 'lib/translatomatic/config.rb', line 136 def project_path if @project_settings_path File.realpath(File.join(File.dirname(@project_settings_path), "..")) else nil end end |
#remove(key, context = nil) ⇒ void
This method returns an undefined value.
Remove a configuration setting
81 82 83 84 85 86 87 |
# File 'lib/translatomatic/config.rb', line 81 def remove(key, context = nil) key = check_valid_key(key) context ||= default_context context = check_valid_context(context) @settings[context].delete(key) save end |
#reset ⇒ Object
Reset all configuration to the defaults
121 122 123 124 |
# File 'lib/translatomatic/config.rb', line 121 def reset @settings = {} CONTEXTS.each { |context| @settings[context] = {} } end |
#save ⇒ Object
Save configuration settings
108 109 110 111 |
# File 'lib/translatomatic/config.rb', line 108 def save save_context(:user, @user_settings_path) save_context(:project, @project_settings_path) end |
#set(key, value, context = nil) ⇒ String
Change a configuration setting. The default context is project level
if a project configuration file exists, otherwise user level.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/translatomatic/config.rb', line 34 def set(key, value, context = nil) key = check_valid_key(key) option = option(key) raise t("config.command_line_only") if option.command_line_only context ||= default_context context = :user if option.user_context_only || key.to_s.match(/api_key/) context = check_valid_context(context) @settings[context][key] = cast(value, option.type) save value end |