Class: Translatomatic::Config::Settings
- Inherits:
-
Object
- Object
- Translatomatic::Config::Settings
- Defined in:
- lib/translatomatic/config/settings.rb
Overview
Translatomatic configuration settings. get and set methods accept a params hash, which recognises the following keys:
-
location: [Symbol] configuration location (:user or :project)
-
for_file: [String] file path for per-file configuration
Instance Attribute Summary collapse
-
#project_path ⇒ String
readonly
The path to the project home.
-
#user_path ⇒ String
readonly
The path to the user home.
Instance Method Summary collapse
-
#add(key, value, params = {}) ⇒ void
If key is an array type, adds the value to the existing list.
-
#all(params = {}) ⇒ Object
Get all configuration settings.
-
#get(key, params = {}) ⇒ String
Get a configuration setting.
-
#include?(key, params = {}) ⇒ boolean
Test if configuration includes the given key.
-
#initialize(options = {}) ⇒ Settings
constructor
A new instance of Settings.
-
#set(key, value, params = {}) ⇒ void
Change a configuration setting.
-
#subtract(key, value, params = {}) ⇒ void
If key is an array type, removes the value from the existing list.
-
#unset(key, params = {}) ⇒ void
Remove a configuration setting.
Constructor Details
#initialize(options = {}) ⇒ Settings
Returns a new instance of Settings.
15 16 17 18 19 20 21 |
# File 'lib/translatomatic/config/settings.rb', line 15 def initialize( = {}) @runtime = [:runtime] || {} @user_path = File.realpath([:user_path] || Dir.home) @project_path = [:project_path] @project_path ||= Files.find_project(@user_path) load end |
Instance Attribute Details
#project_path ⇒ String (readonly)
Returns The path to the project home.
13 14 15 |
# File 'lib/translatomatic/config/settings.rb', line 13 def project_path @project_path end |
#user_path ⇒ String (readonly)
Returns The path to the user home.
10 11 12 |
# File 'lib/translatomatic/config/settings.rb', line 10 def user_path @user_path end |
Instance Method Details
#add(key, value, params = {}) ⇒ void
This method returns an undefined value.
If key is an array type, adds the value to the existing list. Raises an error for non array types.
65 66 67 68 |
# File 'lib/translatomatic/config/settings.rb', line 65 def add(key, value, params = {}) settings_write(key, params).add(key, value) save end |
#all(params = {}) ⇒ Object
Get all configuration settings
83 84 85 86 87 88 89 |
# File 'lib/translatomatic/config/settings.rb', line 83 def all(params = {}) settings = {} Options..each_value do |option| settings[option.name] = get(option.name, params) end settings end |
#get(key, params = {}) ⇒ String
Get a configuration setting
29 30 31 32 33 34 35 36 37 |
# File 'lib/translatomatic/config/settings.rb', line 29 def get(key, params = {}) option = Options.option(key) settings = settings_read(key, params) value = settings ? settings.get(key, option.default) : option.default # cast value to expected type. base_path = config_base_path(settings.location) if settings cast(value, option.type, base_path: base_path) end |
#include?(key, params = {}) ⇒ boolean
Test if configuration includes the given key
95 96 97 98 |
# File 'lib/translatomatic/config/settings.rb', line 95 def include?(key, params = {}) settings = settings_read(key, params) settings && settings.include?(key) end |
#set(key, value, params = {}) ⇒ void
This method returns an undefined value.
Change a configuration setting. By default the project configuration
file is changed if a project configuration file exists,
otherwise the user configuration file is changed.
45 46 47 48 |
# File 'lib/translatomatic/config/settings.rb', line 45 def set(key, value, params = {}) settings_write(key, params).set(key, value) save end |
#subtract(key, value, params = {}) ⇒ void
This method returns an undefined value.
If key is an array type, removes the value from the existing list. Raises an error for non array types.
76 77 78 79 |
# File 'lib/translatomatic/config/settings.rb', line 76 def subtract(key, value, params = {}) settings_write(key, params).subtract(key, value) save end |
#unset(key, params = {}) ⇒ void
This method returns an undefined value.
Remove a configuration setting
54 55 56 57 |
# File 'lib/translatomatic/config/settings.rb', line 54 def unset(key, params = {}) settings_write(key, params).unset(key) save end |