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::AppName)
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) unless File.directory?(CONF_DIR)
  reload
  save unless File.exist?(self.class::CONF_FILE)
end

Instance Method Details

#base_confObject



28
29
30
# File 'lib/core/config/config.rb', line 28

def base_conf
  {}
end

#default_confObject



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_confObject



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

#reloadObject



17
18
19
20
# File 'lib/core/config/config.rb', line 17

def reload
  @conf = merge!(default_conf, user_conf)
  save
end

#saveObject



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

#user_confObject



42
43
44
# File 'lib/core/config/config.rb', line 42

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