Class: Logaling::Config
- Inherits:
-
Object
- Object
- Logaling::Config
- Defined in:
- lib/logaling/config.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(key, value) ⇒ Object
- #check_required_option(required = {}) ⇒ Object
- #glossary ⇒ Object
-
#initialize(config = {}) ⇒ Config
constructor
A new instance of Config.
- #load(config_path = nil) ⇒ Object
- #merge!(config) ⇒ Object
- #save(config_path) ⇒ Object
- #source_language ⇒ Object
- #target_language ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Config
Returns a new instance of Config.
26 27 28 |
# File 'lib/logaling/config.rb', line 26 def initialize(config={}) @config = config end |
Class Method Details
.load(config_path) ⇒ Object
19 20 21 22 23 |
# File 'lib/logaling/config.rb', line 19 def load(config_path) config = new config.load(config_path) config end |
Instance Method Details
#add(key, value) ⇒ Object
52 53 54 55 |
# File 'lib/logaling/config.rb', line 52 def add(key, value) raise Logaling::CommandFailed, "#{key} is unsupported option" unless support?(key) merge!(key => value) end |
#check_required_option(required = {}) ⇒ Object
30 31 32 33 34 |
# File 'lib/logaling/config.rb', line 30 def check_required_option(required={}) required.each do |required_option, | raise(Logaling::CommandFailed, ) unless @config[required_option] end end |
#glossary ⇒ Object
65 66 67 |
# File 'lib/logaling/config.rb', line 65 def glossary @config["glossary"] end |
#load(config_path = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/logaling/config.rb', line 42 def load(config_path=nil) if config_path && File.exist?(config_path) File.readlines(config_path).map{|l| l.chomp.split " "}.each do |option| key = option[0].sub(/^[\-]{2}/, "") value = option[1] @config[key] = value end end end |
#merge!(config) ⇒ Object
36 37 38 39 40 |
# File 'lib/logaling/config.rb', line 36 def merge!(config) keys.each do |key| @config[key] = config[key] if config[key] end end |
#save(config_path) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/logaling/config.rb', line 57 def save(config_path) File.open(config_path, 'w') do |fp| keys.each do |key| fp.puts "--#{key} #{@config[key]}" if @config[key] end end end |
#source_language ⇒ Object
69 70 71 |
# File 'lib/logaling/config.rb', line 69 def source_language @config["source-language"] end |
#target_language ⇒ Object
73 74 75 |
# File 'lib/logaling/config.rb', line 73 def target_language @config["target-language"] end |