| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
require 'environment' |
| 7 |
require 'serialthread' |
| 8 |
require 'miku/miku' |
| 9 |
require 'lib/timelimitedqueue' |
| 10 |
|
| 11 |
require 'fileutils' |
| 12 |
require 'set' |
| 13 |
require 'yaml' |
| 14 |
|
| 15 |
|
| 16 |
ータ保存機能をつけるmix-in |
| 17 |
データを保存するためのメソッドが提供される。 |
| 18 |
常はUserConfigをつかうこと。 |
| 19 |
|
| 20 |
module ConfigLoader |
| 21 |
extend Memoist |
| 22 |
|
| 23 |
STORAGE_FILE = File.expand_path(File.join(Environment::SETTINGDIR, "setting.yml")) |
| 24 |
TMP_FILE = File.expand_path(File.join(Environment::SETTINGDIR, "setting.writing.yml")) |
| 25 |
PSTORE_FILE = File.expand_path(File.join(Environment::CONFROOT, "p_class_values.db")) |
| 26 |
AVAILABLE_TYPES = [Hash, Array, Set, Numeric, String, Symbol, Time, NilClass, TrueClass, FalseClass].freeze |
| 27 |
|
| 28 |
@@configloader_cache = nil |
| 29 |
@@configloader_queue ||= TimeLimitedQueue.new(HYDE, 5, Set){ |keys| |
| 30 |
File.open(TMP_FILE, 'w'.freeze){ |tmpfile| |
| 31 |
YAML.dump(@@configloader_cache, tmpfile) |
| 32 |
} |
| 33 |
FileUtils.mv TMP_FILE, STORAGE_FILE |
| 34 |
notice "configloader: wrote #{keys.size} keys (#{keys.to_a.join(', ')})" } |
| 35 |
|
| 36 |
class << self |
| 37 |
|
| 38 |
ばれる(このソースファイルの一番下の方) |
| 39 |
設定データを読み込む。 |
| 40 |
形式(PStore)からデータを読み込む。 |
| 41 |
def boot |
| 42 |
@@configloader_cache = if FileTest.exist?(STORAGE_FILE) |
| 43 |
notice "load setting data from #{STORAGE_FILE}" |
| 44 |
if YAML.respond_to?(:safe_load_file) |
| 45 |
以降必須) |
| 46 |
YAML.safe_load_file(STORAGE_FILE, permitted_classes: AVAILABLE_TYPES) |
| 47 |
else |
| 48 |
以前 |
| 49 |
YAML.load_file(STORAGE_FILE) |
| 50 |
end |
| 51 |
elsif FileTest.exist?(PSTORE_FILE) |
| 52 |
notice "load setting data from #{PSTORE_FILE}" |
| 53 |
ConfigLoader.migration_from_pstore |
| 54 |
else |
| 55 |
notice "setting data not found" |
| 56 |
Hash.new end end |
| 57 |
|
| 58 |
からデータを取得して返す |
| 59 |
|
| 60 |
ータ(Hash) |
| 61 |
def migration_from_pstore |
| 62 |
require 'pstore' |
| 63 |
PStore.new(PSTORE_FILE).transaction(true) { |db| |
| 64 |
config = Hash.new |
| 65 |
db.roots.each { |key| |
| 66 |
config[key] = db[key] } |
| 67 |
config } end |
| 68 |
|
| 69 |
うでなければ _ArgumentError_ 例外を投げる。 |
| 70 |
def validate(obj) |
| 71 |
if AVAILABLE_TYPES.any?{|x| obj.is_a?(x)} |
| 72 |
if obj.is_a? Hash |
| 73 |
result = {} |
| 74 |
obj.each{ |key, value| |
| 75 |
result[self.validate(key)] = self.validate(value) } |
| 76 |
result.freeze |
| 77 |
elsif obj.is_a? Enumerable |
| 78 |
obj.map(&method(:validate)).freeze |
| 79 |
elsif not(obj.freezable?) or obj.frozen? |
| 80 |
obj |
| 81 |
else |
| 82 |
obj.dup.freeze end |
| 83 |
else |
| 84 |
emes = "ConfigLoader recordable class of #{AVAILABLE_TYPES.join(',')} only. but #{obj.class} given." |
| 85 |
error(emes) |
| 86 |
raise ArgumentError.new(emes) |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
end |
| 91 |
|
| 92 |
る値が存在するかを調べる。 |
| 93 |
_ や _false_ であっても _true_ を返す |
| 94 |
|
| 95 |
キー |
| 96 |
|
| 97 |
在する |
| 98 |
在しない |
| 99 |
def include?(key) |
| 100 |
@@configloader_cache.has_key? configloader_key(key) end |
| 101 |
|
| 102 |
オブジェクトを取り出す。 |
| 103 |
nil か _ifnone_ を返す |
| 104 |
def at(key, ifnone=nil) |
| 105 |
ckey = configloader_key(key) |
| 106 |
if @@configloader_cache.has_key? ckey |
| 107 |
@@configloader_cache[ckey] |
| 108 |
else |
| 109 |
ifnone end end |
| 110 |
|
| 111 |
l_ を関連付ける。 |
| 112 |
def store(key, val) |
| 113 |
ConfigLoader.validate(key) |
| 114 |
val = ConfigLoader.validate(val) |
| 115 |
ckey = configloader_key(key) |
| 116 |
@@configloader_cache[ckey] = val |
| 117 |
@@configloader_queue.push(ckey) |
| 118 |
val end |
| 119 |
|
| 120 |
private |
| 121 |
|
| 122 |
def configloader_key(key) |
| 123 |
"#{self.class.to_s}::#{key}".freeze end |
| 124 |
memoize :configloader_key |
| 125 |
|
| 126 |
boot |
| 127 |
|
| 128 |
end |