Class: Translatomatic::Config::LocationSettings
- Inherits:
-
Object
- Object
- Translatomatic::Config::LocationSettings
- Defined in:
- lib/translatomatic/config/location_settings.rb
Overview
settings for a specific location location can be user settings, project settings, or file specific settings.
Instance Attribute Summary collapse
-
#location ⇒ Symbol
readonly
The location of these settings (:user, :project, :env).
-
#path ⇒ String
readonly
The path to the settings file.
Class Method Summary collapse
-
.from_environment ⇒ Object
load settings from environment variables.
- .runtime(data = {}) ⇒ Object
Instance Method Summary collapse
-
#add(key, value) ⇒ Object
If key is an array type, adds the value to the existing list.
-
#files ⇒ Hash
Files data.
-
#get(key, default = nil) ⇒ String
Get a configuration setting.
-
#include?(key) ⇒ boolean
Test if configuration includes the given key.
-
#initialize(data = {}, options = {}) ⇒ LocationSettings
constructor
A new instance of LocationSettings.
-
#set(key, value) ⇒ Object
Change a configuration setting.
-
#subtract(key, value) ⇒ Object
If key is an array type, removes the value from the existing list.
-
#to_yaml ⇒ String
Configuration as YAML.
-
#unset(key) ⇒ void
Remove a configuration setting.
Constructor Details
#initialize(data = {}, options = {}) ⇒ LocationSettings
Returns a new instance of LocationSettings.
30 31 32 33 34 35 36 |
# File 'lib/translatomatic/config/location_settings.rb', line 30 def initialize(data = {}, = {}) @data = data @options = || {} @location = @options[:location] @path = @options[:path] @data[:files] ||= {} unless [:no_files] end |
Instance Attribute Details
#location ⇒ Symbol (readonly)
Returns The location of these settings (:user, :project, :env).
28 29 30 |
# File 'lib/translatomatic/config/location_settings.rb', line 28 def location @location end |
#path ⇒ String (readonly)
Returns The path to the settings file.
25 26 27 |
# File 'lib/translatomatic/config/location_settings.rb', line 25 def path @path end |
Class Method Details
.from_environment ⇒ Object
load settings from environment variables
9 10 11 12 13 14 15 16 17 |
# File 'lib/translatomatic/config/location_settings.rb', line 9 def from_environment settings = {} Options..each_value do |option| if option.env_name && ENV.include?(option.env_name) settings[option.name] = ENV[option.env_name] end end new(settings, location: :env, path: Dir.pwd) end |
.runtime(data = {}) ⇒ Object
19 20 21 |
# File 'lib/translatomatic/config/location_settings.rb', line 19 def runtime(data = {}) new(data.deep_symbolize_keys, location: :runtime, path: Dir.pwd) end |
Instance Method Details
#add(key, value) ⇒ Object
If key is an array type, adds the value to the existing list. Raises an error for non array types.
75 76 77 |
# File 'lib/translatomatic/config/location_settings.rb', line 75 def add(key, value) update_array(key, value, :add) end |
#files ⇒ Hash
Returns Files data.
44 45 46 |
# File 'lib/translatomatic/config/location_settings.rb', line 44 def files @data[:files] end |
#get(key, default = nil) ⇒ String
Get a configuration setting
51 52 53 |
# File 'lib/translatomatic/config/location_settings.rb', line 51 def get(key, default = nil) include?(key) ? @data[key.to_sym] : default end |
#include?(key) ⇒ boolean
Test if configuration includes the given key
91 92 93 94 |
# File 'lib/translatomatic/config/location_settings.rb', line 91 def include?(key) Options.check_valid_key(key) @data.include?(key.to_sym) end |
#set(key, value) ⇒ Object
Change a configuration setting.
59 60 61 |
# File 'lib/translatomatic/config/location_settings.rb', line 59 def set(key, value) update(key) { |option| @data[option.name] = cast(value, option.type) } end |
#subtract(key, value) ⇒ Object
If key is an array type, removes the value from the existing list. Raises an error for non array types.
84 85 86 |
# File 'lib/translatomatic/config/location_settings.rb', line 84 def subtract(key, value) update_array(key, value, :subtract) end |
#to_yaml ⇒ String
Returns Configuration as YAML.
39 40 41 |
# File 'lib/translatomatic/config/location_settings.rb', line 39 def to_yaml data_for_save.to_yaml end |
#unset(key) ⇒ void
This method returns an undefined value.
Remove a configuration setting
66 67 68 |
# File 'lib/translatomatic/config/location_settings.rb', line 66 def unset(key) update(key) { |option| @data.delete(option.name) } end |