Module: Konjac::Config

Defined in:
lib/konjac/config.rb

Overview

User settings

Constant Summary collapse

CONFIG_PATH =

The path to the user’s configuration file

File.expand_path("~/.konjac/config.yml")

Class Method Summary collapse

Class Method Details

.current?(dict) ⇒ Boolean

Determines whether the specified name of a dictionary is the one currently set as the default

Returns:

  • (Boolean)


43
44
45
# File 'lib/konjac/config.rb', line 43

def current?(dict)
  dict == dictionary
end

.dictionaryObject

The current dictionary



37
38
39
# File 'lib/konjac/config.rb', line 37

def dictionary
  @config[:dict] || "dict"
end

.languageObject

The current interface language



54
55
56
# File 'lib/konjac/config.rb', line 54

def language
  I18n.locale
end

.language=(lang) ⇒ Object

Sets the language to use for the interface. Note that this has no effect in determining the to or from languages to use for translation



60
61
62
63
# File 'lib/konjac/config.rb', line 60

def language=(lang)
  set_language lang, I18n.locale
  save
end

.listObject

Lists the dictionaries available in ~/.konjac/*.yml



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/konjac/config.rb', line 22

def list
  lines = []
  current_found = false
  Dir[File.expand_path("~/.konjac/*.yml")].each do |dict|
    name = File.basename(dict, ".*")
    unless name == "config"
      current_found = true
      lines << "%s %s" % [current?(name) ? "***" : "   ", name]
    end
  end
  lines << "*** %s" % dictionary unless current_found
  lines
end

.loadObject

Loads the user’s settings



12
13
14
15
16
17
18
19
# File 'lib/konjac/config.rb', line 12

def load
  Utils.verify_file CONFIG_PATH, "--- {}"
  @config = YAML.load_file(CONFIG_PATH)
  @config = {} unless @config.is_a?(Hash)

  set_language @config[:language], (ENV["LANG"][0..1] rescue ""), :en
  save
end

.saveObject

Saves the user configurations



66
67
68
69
70
# File 'lib/konjac/config.rb', line 66

def save
  File.open(CONFIG_PATH, "w") do |file|
    YAML.dump @config, file
  end
end

.use(dict) ⇒ Object

Specifies the name of the dictionary to use



48
49
50
51
# File 'lib/konjac/config.rb', line 48

def use(dict)
  @config[:dict] = dict.to_s unless dict.to_s == "config"
  save
end