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::CConf[:app][:name])
- 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
- #module_conf ⇒ Object
- #parse_dir(path) ⇒ Object
- #pref_model(title, value, trail) ⇒ Object
- #reload ⇒ Object
- #save ⇒ Object
- #save? ⇒ Boolean
- #set(trail, destination) ⇒ Object
- #user_conf ⇒ Object
Constructor Details
Instance Method Details
#base_conf ⇒ Object
26 27 28 |
# File 'lib/core/config/config.rb', line 26 def base_conf {} end |
#default_conf ⇒ Object
30 31 32 33 34 |
# File 'lib/core/config/config.rb', line 30 def default_conf module_conf.reduce(base_conf) do |mem, mc| Zashoku::Util.deep_merge(mem, mc) end end |
#get(trail) ⇒ Object
84 85 86 |
# File 'lib/core/config/config.rb', line 84 def get(trail) @conf.dig(*trail) end |
#items(c = @conf, pre = '', path = []) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/core/config/config.rb', line 48 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 |
#module_conf ⇒ Object
36 37 38 |
# File 'lib/core/config/config.rb', line 36 def module_conf {} end |
#parse_dir(path) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/core/config/config.rb', line 68 def parse_dir(path) { '$conf_dir' => CONF_DIR, '~' => Dir.home }.inject(path) { |p, r| p.gsub(r[0], r[1]) } end |
#pref_model(title, value, trail) ⇒ Object
44 45 46 |
# File 'lib/core/config/config.rb', line 44 def pref_model(title, value, trail) Item.new('title' => title, 'value' => value, 'trail' => trail) end |
#reload ⇒ Object
21 22 23 24 |
# File 'lib/core/config/config.rb', line 21 def reload @conf = Zashoku::Util.deep_merge(default_conf, user_conf) save end |
#save ⇒ Object
88 89 90 91 |
# File 'lib/core/config/config.rb', line 88 def save return unless save? File.open(self.class::CONF_FILE, 'w') { |f| f.write(YAML.dump(@conf)) } end |
#save? ⇒ Boolean
17 18 19 |
# File 'lib/core/config/config.rb', line 17 def save? Zashoku::CConf[:app][:save_config] end |
#set(trail, destination) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/core/config/config.rb', line 75 def set(trail, destination) trail = [trail] if trail.class == String trail[0...-1].inject(@conf, :fetch)[trail.last] = destination save changed notify_observers end |