Class: Zashoku::Config
- Inherits:
-
Controller
- Object
- Controller
- Zashoku::Config
- Defined in:
- lib/core/config/config.rb
Direct Known Subclasses
Constant Summary collapse
- CONF_DIR =
File.join(Dir.home, '.config/', Zashoku::AppName)
- CONF_FILE =
File.join(CONF_DIR, '/conf.yml')
Instance Method Summary collapse
- #base_conf ⇒ Object
- #default_conf ⇒ Object
- #get(trail) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #items(c = @conf, pre = '', path = []) ⇒ Object
- #merge!(this, that) ⇒ Object
- #module_conf ⇒ Object
- #parse_dir(path) ⇒ Object
- #pref_model(title, value, trail) ⇒ Object
- #reload ⇒ Object
- #save ⇒ Object
- #set(trail, destination) ⇒ Object
- #user_conf ⇒ Object
Constructor Details
Instance Method Details
#base_conf ⇒ Object
28 29 30 |
# File 'lib/core/config/config.rb', line 28 def base_conf {} end |
#default_conf ⇒ Object
32 33 34 35 36 |
# File 'lib/core/config/config.rb', line 32 def default_conf module_conf.reduce(base_conf) do |mem, mc| merge!(mem, mc) end end |
#get(trail) ⇒ Object
87 88 89 |
# File 'lib/core/config/config.rb', line 87 def get(trail) @conf.dig(*trail) end |
#items(c = @conf, pre = '', path = []) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/core/config/config.rb', line 50 def items(c = @conf, pre = '', path = []) return pref_model(pre, c, path) unless c.respond_to?(:map) c.map do |title, pref| # next if IGNORE.key? title t = "#{pre}_#{title}" pth = path + [title] case pref when Hash pref.map do |subt, subpref| items(subpref, "#{pre}_#{title}_#{subt}", [title, subt]) #pref_model("#{title}_#{subt}", subpref, [title, subt]) end when Array pref_model(t, pref.join(':'), pth) else pref_model(t, pref, pth) end end.flatten #.compact end |
#merge!(this, that) ⇒ Object
22 23 24 25 26 |
# File 'lib/core/config/config.rb', line 22 def merge!(this, that) this&.merge(that || {}) do |_k, old, nu| old.class == Hash ? merge!(old, nu) : nu end end |
#module_conf ⇒ Object
38 39 40 |
# File 'lib/core/config/config.rb', line 38 def module_conf {} end |
#parse_dir(path) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/core/config/config.rb', line 70 def parse_dir(path) { # '$airing_dir' => @conf['airing_dir'], # '$conf_dir' => @conf['conf_dir'], '~' => Dir.home }.inject(path) { |p, r| p.gsub(r[0], r[1]) } end |
#pref_model(title, value, trail) ⇒ Object
46 47 48 |
# File 'lib/core/config/config.rb', line 46 def pref_model(title, value, trail) Item.new('title' => title, 'value' => value, 'trail' => trail) end |
#reload ⇒ Object
17 18 19 20 |
# File 'lib/core/config/config.rb', line 17 def reload @conf = merge!(default_conf, user_conf) save end |
#save ⇒ Object
91 92 93 |
# File 'lib/core/config/config.rb', line 91 def save File.open(self.class::CONF_FILE, 'w') { |f| f.write(YAML.dump(@conf)) } end |
#set(trail, destination) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/core/config/config.rb', line 78 def set(trail, destination) trail = [trail] if trail.class == String trail[0...-1].inject(@conf, :fetch)[trail.last] = destination save changed notify_observers end |