Class: Translatomatic::Config

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/config.rb

Overview

Translatomatic configuration. Configuration settings may be specified in the following locations:

- environment variables
- user configuration file $HOME/.translatomatic/config.yml
- project configuration file $PROJECT/.translatomatic/config.yml
- command line options

Settings are read in the order given above, with last setting found taking precedence over values read earlier.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_localeString

Returns The default locale.

Returns:

  • (String)

    The default locale



20
21
22
# File 'lib/translatomatic/config.rb', line 20

def default_locale
  @default_locale
end

#loggerLogger

Returns The logger instance.

Returns:

  • (Logger)

    The logger instance



17
18
19
# File 'lib/translatomatic/config.rb', line 17

def logger
  @logger
end

#project_settings_pathString

Returns The path to the project settings file.

Returns:

  • (String)

    The path to the project settings file



26
27
28
# File 'lib/translatomatic/config.rb', line 26

def project_settings_path
  @project_settings_path
end

#user_settings_pathString

Returns The path to the user settings file.

Returns:

  • (String)

    The path to the user settings file



23
24
25
# File 'lib/translatomatic/config.rb', line 23

def 
  @user_settings_path
end

Class Method Details

.optionsArray<Translatomatic::Option] all available options

Returns Array<Translatomatic::Option] all available options.

Returns:



127
128
129
# File 'lib/translatomatic/config.rb', line 127

def self.options
  self.config_options.values
end

Instance Method Details

#get(key, context = nil) ⇒ String

Get a configuration setting

Parameters:

  • key (String)

    configuration key

  • context (Symbol) (defaults to: nil)

    configuration context. May be nil.

Returns:

  • (String)

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/translatomatic/config.rb', line 52

def get(key, context = nil)
  key = check_valid_key(key)
  option = option(key)
  value = option.default  # set to default value

  if context.nil?
    # find the first setting in the following order
    CONTEXTS.each do |ctx|
      if @settings[ctx].include?(key)
        value = @settings[ctx][key]
        break
      end
    end
  else
    # context is set
    context = check_valid_context(context)
    if @settings[context].include?(key)
      value = @settings[context][key]
    end
  end

  # cast value to expected type
  cast(value, option.type)
end

#include?(key, context = nil) ⇒ String, boolean

Test if configuration includes the given key

Parameters:

  • key (String)

    configuration key

Returns:

  • (String)

    The configuration value. If context is nil, checks all contexts.

  • (boolean)

    true if the configuration key is set



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/translatomatic/config.rb', line 94

def include?(key, context = nil)
  key = check_valid_key(key)
  if context.nil?
    CONTEXTS.each do |ctx|
      return true if @settings[ctx].include?(key)
    end
    false
  else
    context = check_valid_context(context)
    @settings[context].include?(key)
  end
end

#loadObject

Load configuration from the config file(s)



114
115
116
117
118
# File 'lib/translatomatic/config.rb', line 114

def load
  load_context_env
  load_context(:user, @user_settings_path)
  load_context(:project, @project_settings_path)
end

#project_pathObject

The project path is found by searching for a ‘.translatomatic’ directory that is not within the user home directory. The search ascends upwards from the current working directory.

Returns:

  • The path to the current project, or nil if the current project path is unknown.



136
137
138
139
140
141
142
# File 'lib/translatomatic/config.rb', line 136

def project_path
  if @project_settings_path
    File.realpath(File.join(File.dirname(@project_settings_path), ".."))
  else
    nil
  end
end

#remove(key, context = nil) ⇒ void

This method returns an undefined value.

Remove a configuration setting

Parameters:

  • key (String)

    configuration key to remove

  • context (Symbol) (defaults to: nil)

    configuration context



81
82
83
84
85
86
87
# File 'lib/translatomatic/config.rb', line 81

def remove(key, context = nil)
  key = check_valid_key(key)
  context ||= default_context
  context = check_valid_context(context)
  @settings[context].delete(key)
  save
end

#resetObject

Reset all configuration to the defaults



121
122
123
124
# File 'lib/translatomatic/config.rb', line 121

def reset
  @settings = {}
  CONTEXTS.each { |context| @settings[context] = {} }
end

#saveObject

Save configuration settings



108
109
110
111
# File 'lib/translatomatic/config.rb', line 108

def save
  save_context(:user, @user_settings_path)
  save_context(:project, @project_settings_path)
end

#set(key, value, context = nil) ⇒ String

Change a configuration setting. The default context is project level

if a project configuration file exists, otherwise user level.

Parameters:

  • key (String)

    configuration key

  • value (String)

    new value for the configuration

  • context (Symbol) (defaults to: nil)

    configuration context

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/translatomatic/config.rb', line 34

def set(key, value, context = nil)
  key = check_valid_key(key)
  option = option(key)
  raise t("config.command_line_only") if option.command_line_only
  context ||= default_context
  context = :user if option.user_context_only || key.to_s.match(/api_key/)
  context = check_valid_context(context)
  @settings[context][key] = cast(value, option.type)
  save
  value
end