class Translatomatic::Config::LocationSettings
settings for a specific location location can be user settings, project settings, or file specific settings.
Attributes
@return [Symbol] The location of these settings (:user, :project, :env)
@return [String] The path to the settings file
Public Class Methods
load settings from environment variables
# File lib/translatomatic/config/location_settings.rb, line 9 def from_environment settings = {} Options.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
# File lib/translatomatic/config/location_settings.rb, line 30 def initialize(data = {}, options = {}) @data = data @options = options || {} @location = @options[:location] @path = @options[:path] @data[:files] ||= {} unless options[:no_files] end
# File lib/translatomatic/config/location_settings.rb, line 19 def runtime(data = {}) new(data.deep_symbolize_keys, location: :runtime, path: Dir.pwd) end
Public Instance Methods
If key is an array type, adds the value to the existing list. Raises an error for non array types. @param key [String] configuration key @param value [Object] value to add to the list @return [Object] the new value
# File lib/translatomatic/config/location_settings.rb, line 75 def add(key, value) update_array(key, value, :add) end
@return [Hash] Files
data
# File lib/translatomatic/config/location_settings.rb, line 44 def files @data[:files] end
Get a configuration setting @param key [String] configuration key @return [String] The configuration value
# File lib/translatomatic/config/location_settings.rb, line 51 def get(key, default = nil) include?(key) ? @data[key.to_sym] : default end
Test if configuration includes the given key @param key [String] configuration key @return [boolean] true if the configuration key is set
# File lib/translatomatic/config/location_settings.rb, line 91 def include?(key) Options.check_valid_key(key) @data.include?(key.to_sym) end
Change a configuration setting. @param key [String] configuration key @param value [Object] new value for the configuration @return [Object] the new value
# File lib/translatomatic/config/location_settings.rb, line 59 def set(key, value) update(key) { |option| @data[option.name] = cast(value, option.type) } end
If key is an array type, removes the value from the existing list. Raises an error for non array types. @param key [String] configuration key @param value [Object] value to remove from the list @return [Object] the new value
# File lib/translatomatic/config/location_settings.rb, line 84 def subtract(key, value) update_array(key, value, :subtract) end
@return [String] Configuration as YAML
# File lib/translatomatic/config/location_settings.rb, line 39 def to_yaml data_for_save.to_yaml end
Remove a configuration setting @param key [String] configuration key to remove @return [void]
# File lib/translatomatic/config/location_settings.rb, line 66 def unset(key) update(key) { |option| @data.delete(option.name) } end
Private Instance Methods
# File lib/translatomatic/config/location_settings.rb, line 127 def assert_array_type(option) is_array = array_type?(option.type) raise t('config.non_array_key', key: option.name) unless is_array end
# File lib/translatomatic/config/location_settings.rb, line 101 def data_for_save data = @data.dup files = data.delete(:files) # put files at the bottom data[:files] = files unless files.blank? data end
common functionality for set/unset/add/subtract methods
# File lib/translatomatic/config/location_settings.rb, line 120 def update(key) key = Options.check_valid_key(key) option = Options.options[key.to_sym] raise t('config.command_line_only') if option.command_line_only yield option end
# File lib/translatomatic/config/location_settings.rb, line 109 def update_array(key, value, add) update(key) do |option| assert_array_type(option) current = @data[option.name] || [] casted = cast(value, option.type) new_value = add == :add ? current + casted : current - casted @data[option.name] = new_value end end