Sha256: 33047a2bc964e75d38d5b63aedd06002997c2d888e80e89bf0277b24abb32c61

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# -*- coding: UTF-8 -*-
#
# Copyright 2013 whiteleaf. All rights reserved.
#

require "yaml"
require "singleton"

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

  def self.get
    instance
  end

  def initialize
    @global_settings = load_settings
  end

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

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

  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

  #
  # .narou 以下に保存されている *.yaml の拡張子を取った部分をキーとして
  # 設定をハッシュとして取得する
  #
  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
narou-1.1.1 lib/globalsetting.rb
narou-1.1.0 lib/globalsetting.rb
narou-1.1.0.rc2 lib/globalsetting.rb
narou-1.1.0.rc1 lib/globalsetting.rb
narou-1.0.2 lib/globalsetting.rb
narou-1.0.1 lib/globalsetting.rb
narou-1.0.0 lib/globalsetting.rb