Class: Zashoku::Config

Inherits:
Controller show all
Defined in:
lib/core/config/config.rb

Direct Known Subclasses

DaemonConfig, ViewConfig

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

Constructor Details

#initializeConfig

Returns a new instance of Config.



11
12
13
14
15
# File 'lib/core/config/config.rb', line 11

def initialize
  FileUtils.mkdir_p(CONF_DIR) if save? && !File.directory?(CONF_DIR)
  reload
  save unless File.exist?(self.class::CONF_FILE)
end

Instance Method Details

#base_confObject



26
27
28
# File 'lib/core/config/config.rb', line 26

def base_conf
  {}
end

#default_confObject



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_confObject



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

#reloadObject



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

#saveObject



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

Returns:

  • (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

#user_confObject



40
41
42
# File 'lib/core/config/config.rb', line 40

def user_conf
  Util.get_yaml(self.class::CONF_FILE)
end