Class: Translatomatic::Config::Settings

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

Instance Method Summary collapse

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(options = {})
  @runtime = options[:runtime] || {}
  @user_path = File.realpath(options[:user_path] || Dir.home)
  @project_path = options[:project_path]
  @project_path ||= Files.find_project(@user_path)
  load
end

Instance Attribute Details

#project_pathString (readonly)

Returns The path to the project home.

Returns:

  • (String)

    The path to the project home



13
14
15
# File 'lib/translatomatic/config/settings.rb', line 13

def project_path
  @project_path
end

#user_pathString (readonly)

Returns The path to the user home.

Returns:

  • (String)

    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.

Parameters:

  • key (String)

    configuration key

  • value (Object)

    value to add to the list

  • params (Hash) (defaults to: {})

    options



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

Parameters:

  • params (Hash) (defaults to: {})

    options



83
84
85
86
87
88
89
# File 'lib/translatomatic/config/settings.rb', line 83

def all(params = {})
  settings = {}
  Options.options.each_value do |option|
    settings[option.name] = get(option.name, params)
  end
  settings
end

#get(key, params = {}) ⇒ String

Get a configuration setting

Parameters:

  • key (String)

    configuration key

  • params (Hash) (defaults to: {})

    options

Returns:

  • (String)

    The configuration value. If location is nil, returns the effective value by precedence, otherwise it returns the setting for the given configuration file location.



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

Parameters:

  • key (String)

    configuration key

  • params (Hash) (defaults to: {})

    options

Returns:

  • (boolean)

    true if the configuration key is set



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.

Parameters:

  • key (String)

    configuration key

  • params (Hash) (defaults to: {})

    options



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.

Parameters:

  • key (String)

    configuration key

  • value (Object)

    value to remove from the list

  • params (Hash) (defaults to: {})

    options



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

Parameters:

  • key (String)

    configuration key to remove

  • params (Hash) (defaults to: {})

    options



54
55
56
57
# File 'lib/translatomatic/config/settings.rb', line 54

def unset(key, params = {})
  settings_write(key, params).unset(key)
  save
end