Class: Translatomatic::Config::Files

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

Overview

Configuration file operations. 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.

Class Method Summary collapse

Class Method Details

.find_project(user_path) ⇒ Object

find a project directory working upwards from current directory. stop at user path.



39
40
41
42
43
44
45
46
47
# File 'lib/translatomatic/config/files.rb', line 39

def find_project(user_path)
  user_path = user_path.to_s
  Pathname.new(Dir.pwd).ascend do |v|
    return nil if v.to_s == user_path || user_path.start_with?(v.to_s)
    config_path = v + CONFIG_DIR
    return v if config_path.directory?
  end
  nil
end

.load(path, options = {}) ⇒ LocationSettings

load configuration from the yaml file at path.

Parameters:

  • path

    to config file

Returns:



28
29
30
31
32
33
34
35
# File 'lib/translatomatic/config/files.rb', line 28

def load(path, options = {})
  return nil unless path
  config_path = File.join(path, CONFIG_PATH)
  options = options.merge(path: path)
  return new_settings({}, options) unless File.exist?(config_path)
  config = YAML.load_file(config_path) || {}
  new_settings(config, options)
end

.save(settings) ⇒ void

This method returns an undefined value.

Save location settings to file

Parameters:



18
19
20
21
22
23
# File 'lib/translatomatic/config/files.rb', line 18

def save(settings)
  return unless settings && settings.path
  config_path = File.join(settings.path, CONFIG_PATH)
  FileUtils.mkdir_p(File.dirname(config_path))
  File.write(config_path, settings.to_yaml)
end