Module: VivlioStarter::CLI::IndexCommands::YomiOverrides
- Defined in:
- lib/vivlio_starter/cli/index/yomi_overrides.rb
Constant Summary collapse
- FILE =
'config/index_yomi_overrides.yml'
Class Method Summary collapse
-
.load ⇒ Object
term => yomi のマップを返す(無ければ空)。.
-
.merge!(map, prefer_import: false) ⇒ Array(Integer, Integer)
読み辞書を追記マージする。既定は既存優先(prefer_import で上書き)。.
-
.save(map) ⇒ Object
term 昇順で保存する。.
Class Method Details
.load ⇒ Object
term => yomi のマップを返す(無ければ空)。
33 34 35 36 37 38 39 40 41 |
# File 'lib/vivlio_starter/cli/index/yomi_overrides.rb', line 33 def load return {} unless File.exist?(FILE) data = YAML.load_file(FILE) (data && data['yomi']) || {} rescue StandardError => e Common.log_warn("#{FILE} の読み込みに失敗しました: #{e.}") {} end |
.merge!(map, prefer_import: false) ⇒ Array(Integer, Integer)
読み辞書を追記マージする。既定は既存優先(prefer_import で上書き)。
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vivlio_starter/cli/index/yomi_overrides.rb', line 45 def merge!(map, prefer_import: false) return [0, 0] if map.nil? || map.empty? existing = load written = 0 skipped = 0 map.each do |term, yomi| next if term.to_s.empty? || yomi.to_s.empty? if existing.key?(term) && (!prefer_import || existing[term] == yomi) skipped += 1 else existing[term] = yomi written += 1 end end save(existing) if written.positive? [written, skipped] end |
.save(map) ⇒ Object
term 昇順で保存する。
68 69 70 71 72 |
# File 'lib/vivlio_starter/cli/index/yomi_overrides.rb', line 68 def save(map) FileUtils.mkdir_p(File.dirname(FILE)) data = { 'generated_at' => Time.now.strftime('%Y-%m-%d %H:%M:%S'), 'yomi' => map.sort.to_h } File.write(FILE, data.to_yaml, encoding: 'utf-8') end |