Class: GlobalSetting

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/globalsetting.rb

Overview

ユーザープロファイルに保存するのでいつでも反映される設定

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlobalSetting

Returns a new instance of GlobalSetting.



20
21
22
# File 'lib/globalsetting.rb', line 20

def initialize
  @global_settings = load_settings
end

Class Method Details

.getObject



16
17
18
# File 'lib/globalsetting.rb', line 16

def self.get
  instance
end

Instance Method Details

#[](name) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/globalsetting.rb', line 24

def [](name)
  setting = @global_settings[name]
  unless setting
    setting = {}
    @global_settings[name] = setting
  end
  setting
end

#[]=(name, setting) ⇒ Object



33
34
35
# File 'lib/globalsetting.rb', line 33

def []=(name, setting)
  @global_settings[name] = setting
end

#load_settingsObject

.narou 以下に保存されている *.yaml の拡張子を取った部分をキーとして 設定をハッシュとして取得する



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/globalsetting.rb', line 53

def load_settings
  settings = {}
  global_setting_dir = Narou.get_global_setting_dir
  if global_setting_dir
    Dir.glob(File.join(global_setting_dir, "*.yaml")) do |path|
      name = File.basename(path, ".yaml")
      settings[name] = YAML.load_file(path)
    end
  end
  settings
end

#save_settings(name = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/globalsetting.rb', line 37

def save_settings(name = nil)
  if name
    save_list = { name => @global_settings[name] }
  else
    save_list = @global_settings
  end
  save_list.each do |sname, setting|
    path = File.join(Narou.get_global_setting_dir, sname + ".yaml")
    File.write(path, YAML.dump(setting))
  end
end