Class: Translatomatic::Config::LocationSettings

Inherits:
Object
  • Object
show all
Includes:
TypeCast, Util
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

Class Method Summary collapse

Instance Method Summary collapse

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 = {}, options = {})
  @data = data
  @options = options || {}
  @location = @options[:location]
  @path = @options[:path]
  @data[:files] ||= {} unless options[:no_files]
end

Instance Attribute Details

#locationSymbol (readonly)

Returns The location of these settings (:user, :project, :env).

Returns:

  • (Symbol)

    The location of these settings (:user, :project, :env)



28
29
30
# File 'lib/translatomatic/config/location_settings.rb', line 28

def location
  @location
end

#pathString (readonly)

Returns The path to the settings file.

Returns:

  • (String)

    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_environmentObject

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.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.

Parameters:

  • key (String)

    configuration key

  • value (Object)

    value to add to the list

Returns:

  • (Object)

    the new value



75
76
77
# File 'lib/translatomatic/config/location_settings.rb', line 75

def add(key, value)
  update_array(key, value, :add)
end

#filesHash

Returns Files data.

Returns:

  • (Hash)

    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

Parameters:

  • key (String)

    configuration key

Returns:

  • (String)

    The configuration value



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

Parameters:

  • key (String)

    configuration key

Returns:

  • (boolean)

    true if the configuration key is set



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.

Parameters:

  • key (String)

    configuration key

  • value (Object)

    new value for the configuration

Returns:

  • (Object)

    the new value



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.

Parameters:

  • key (String)

    configuration key

  • value (Object)

    value to remove from the list

Returns:

  • (Object)

    the new value



84
85
86
# File 'lib/translatomatic/config/location_settings.rb', line 84

def subtract(key, value)
  update_array(key, value, :subtract)
end

#to_yamlString

Returns Configuration as YAML.

Returns:

  • (String)

    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

Parameters:

  • key (String)

    configuration key to remove



66
67
68
# File 'lib/translatomatic/config/location_settings.rb', line 66

def unset(key)
  update(key) { |option| @data.delete(option.name) }
end