class Translatomatic::Config::LocationSettings

settings for a specific location location can be user settings, project settings, or file specific settings.

Attributes

location[R]

@return [Symbol] The location of these settings (:user, :project, :env)

path[R]

@return [String] The path to the settings file

Public Class Methods

from_environment() click to toggle source

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
new(data = {}, options = {}) click to toggle source
# 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
runtime(data = {}) click to toggle source
# 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

add(key, value) click to toggle source

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
files() click to toggle source

@return [Hash] Files data

# File lib/translatomatic/config/location_settings.rb, line 44
def files
  @data[:files]
end
get(key, default = nil) click to toggle source

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
include?(key) click to toggle source

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
set(key, value) click to toggle source

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
subtract(key, value) click to toggle source

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
to_yaml() click to toggle source

@return [String] Configuration as YAML

# File lib/translatomatic/config/location_settings.rb, line 39
def to_yaml
  data_for_save.to_yaml
end
unset(key) click to toggle source

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

assert_array_type(option) click to toggle source
# 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
data_for_save() click to toggle source
# 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
update(key) { |option| ... } click to toggle source

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
update_array(key, value, add) click to toggle source
# 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