Class: Translatomatic::Config
- Inherits:
-
Object
- Object
- Translatomatic::Config
- Includes:
- Singleton, Util
- Defined in:
- lib/translatomatic/config.rb
Overview
Translatomatic configuration
Instance Attribute Summary collapse
-
#default_locale ⇒ String
The default locale.
-
#logger ⇒ Logger
The logger instance.
Class Method Summary collapse
-
.options ⇒ Array<Translatomatic::Option] all available options
Array<Translatomatic::Option] all available options.
Instance Method Summary collapse
-
#get(key) ⇒ String
Get a configuration setting.
-
#include?(key) ⇒ boolean
Test if configuration includes the given key.
-
#load ⇒ Object
Load configuration from the config file.
-
#remove(key) ⇒ void
Remove a configuration setting.
-
#save ⇒ Object
Save configuration settings.
-
#set(key, value) ⇒ String
Change a configuration setting.
-
#settings ⇒ Hash<String,String>
Configuration settings.
Instance Attribute Details
#default_locale ⇒ String
Returns The default locale.
12 13 14 |
# File 'lib/translatomatic/config.rb', line 12 def default_locale @default_locale end |
#logger ⇒ Logger
Returns The logger instance.
9 10 11 |
# File 'lib/translatomatic/config.rb', line 9 def logger @logger end |
Class Method Details
.options ⇒ Array<Translatomatic::Option] all available options
Returns Array<Translatomatic::Option] all available options.
77 78 79 |
# File 'lib/translatomatic/config.rb', line 77 def self. self..values end |
Instance Method Details
#get(key) ⇒ String
Get a configuration setting
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/translatomatic/config.rb', line 27 def get(key) key = check_valid_key(key) option = option(key) env_name = option.name.to_s.upcase if @settings.include?(key) cast(@settings[key], option.type) elsif option.use_env && ENV.include?(env_name) cast(ENV[env_name], option.type) else option.default end end |
#include?(key) ⇒ boolean
Test if configuration includes the given key
52 53 54 55 |
# File 'lib/translatomatic/config.rb', line 52 def include?(key) key = check_valid_key(key) @settings.include?(key) end |
#load ⇒ Object
Load configuration from the config file
69 70 71 72 73 74 |
# File 'lib/translatomatic/config.rb', line 69 def load @settings = YAML.load_file(@settings_path) if File.exist?(@settings_path) @settings ||= {} @settings.delete_if { |i| !valid_key?(i) } @settings end |
#remove(key) ⇒ void
This method returns an undefined value.
Remove a configuration setting
43 44 45 46 47 |
# File 'lib/translatomatic/config.rb', line 43 def remove(key) key = check_valid_key(key) @settings.delete(key) save end |
#save ⇒ Object
Save configuration settings
63 64 65 66 |
# File 'lib/translatomatic/config.rb', line 63 def save FileUtils.mkdir_p(File.dirname(@settings_path)) File.open(@settings_path, "w") { |f| f.puts @settings.to_yaml } end |
#set(key, value) ⇒ String
Change a configuration setting
18 19 20 21 22 |
# File 'lib/translatomatic/config.rb', line 18 def set(key, value) key = check_valid_key(key) @settings[key] = value save end |