Class: Vivlio::Starter::CLI::UnifiedTermsManager
- Inherits:
-
Object
- Object
- Vivlio::Starter::CLI::UnifiedTermsManager
- Defined in:
- lib/vivlio/starter/cli/index/unified_terms_manager.rb
Constant Summary collapse
- UNIFIED_FILE =
'config/index_glossary_terms.yml'
Instance Method Summary collapse
-
#clear_cache! ⇒ Object
キャッシュをクリア.
-
#find_term(name) ⇒ Hash?
用語を名前で検索.
-
#glossary_term_names ⇒ Object
用語集対象の用語名.
-
#glossary_terms ⇒ Object
flags に g を含む用語(用語集対象).
-
#index_term_names ⇒ Object
索引対象の用語名.
-
#index_terms ⇒ Object
flags に i を含む用語(索引対象).
-
#initialize ⇒ UnifiedTermsManager
constructor
A new instance of UnifiedTermsManager.
-
#load_terms ⇒ Array<Hash>
全用語を読み込み.
-
#merge_terms!(new_terms, flags: 'i', source: 'auto_extracted') ⇒ Object
用語をマージして保存 同名の用語が既存の場合は更新、なければ追加.
-
#remove_flag!(term_name, remove_flag) ⇒ Object
flags から特定のフラグを除去(用語自体は残す) 例: flags ‘ig’ から ‘i’ を除去 → ‘g’ に変更.
-
#remove_term!(term_name) ⇒ Object
用語を削除.
-
#term_names ⇒ Object
全用語名.
-
#update_backlink_sources!(term_name, sources) ⇒ Object
backlink_sources を更新.
-
#update_definition!(term_name, definition) ⇒ Object
説明文を更新.
-
#update_flags!(term_name, new_flags) ⇒ Object
flags を更新.
-
#update_yomi!(yomi_changes) ⇒ Object
読みを更新.
Constructor Details
#initialize ⇒ UnifiedTermsManager
Returns a new instance of UnifiedTermsManager.
33 34 35 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 33 def initialize @cache = nil end |
Instance Method Details
#clear_cache! ⇒ Object
キャッシュをクリア
216 217 218 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 216 def clear_cache! @cache = nil end |
#find_term(name) ⇒ Hash?
用語を名前で検索
81 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 81 def find_term(name) = load_terms.find { it['term'] == name } |
#glossary_term_names ⇒ Object
用語集対象の用語名
76 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 76 def glossary_term_names = glossary_terms.map { it['term'] } |
#glossary_terms ⇒ Object
flags に g を含む用語(用語集対象)
67 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 67 def glossary_terms = load_terms.select { glossary_flag?(it['flags']) } |
#index_term_names ⇒ Object
索引対象の用語名
73 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 73 def index_term_names = index_terms.map { it['term'] } |
#index_terms ⇒ Object
flags に i を含む用語(索引対象)
64 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 64 def index_terms = load_terms.select { index_flag?(it['flags']) } |
#load_terms ⇒ Array<Hash>
全用語を読み込み
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 41 def load_terms return @cache if @cache unless File.exist?(UNIFIED_FILE) @cache = [] return @cache end begin data = YAML.load_file(UNIFIED_FILE, symbolize_names: false) terms = data['terms'] || [] # 後方互換: flags 未設定のエントリは用語集由来とみなし 'g' を付与 terms.each { it['flags'] ||= 'g' } @cache = terms rescue StandardError => e Common.log_warn("#{UNIFIED_FILE} の読み込みに失敗: #{e.}") @cache = [] end @cache end |
#merge_terms!(new_terms, flags: 'i', source: 'auto_extracted') ⇒ Object
用語をマージして保存同名の用語が既存の場合は更新、なければ追加
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 90 def merge_terms!(new_terms, flags: 'i', source: 'auto_extracted') return if new_terms.nil? || new_terms.empty? existing = load_terms.dup added_count = 0 new_terms.each do |term| term_name = term['term'] || term[:term] next if term_name.nil? || term_name.empty? idx = existing.find_index { it['term'] == term_name } if idx # 既存用語を更新(flags をマージ、データを上書き) existing[idx] = merge_term_data(existing[idx], term, flags) else # 新規追加 existing << build_term_entry(term, flags, source) added_count += 1 end end save_terms!(existing) Common.log_success("#{added_count} 件の用語を追加しました") if added_count.positive? end |
#remove_flag!(term_name, remove_flag) ⇒ Object
flags から特定のフラグを除去(用語自体は残す)例: flags ‘ig’ から ‘i’ を除去 → ‘g’ に変更
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 131 def remove_flag!(term_name, remove_flag) existing = load_terms.dup term = existing.find { it['term'] == term_name } return unless term current = term['flags'] || '' new_flags = case remove_flag when 'i' then current.delete('i') when 'g' then current.delete('g') else current end if new_flags.empty? # フラグがなくなったら用語自体を削除 existing.reject! { it['term'] == term_name } else term['flags'] = new_flags end save_terms!(existing) end |
#remove_term!(term_name) ⇒ Object
用語を削除
117 118 119 120 121 122 123 124 125 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 117 def remove_term!(term_name) return if term_name.nil? || term_name.empty? existing = load_terms.dup original_size = existing.size existing.reject! { it['term'] == term_name } save_terms!(existing) if existing.size < original_size end |
#term_names ⇒ Object
全用語名
70 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 70 def term_names = load_terms.map { it['term'] } |
#update_backlink_sources!(term_name, sources) ⇒ Object
backlink_sources を更新
205 206 207 208 209 210 211 212 213 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 205 def update_backlink_sources!(term_name, sources) existing = load_terms.dup term = existing.find { it['term'] == term_name } return false unless term term['backlink_sources'] = sources save_terms!(existing) true end |
#update_definition!(term_name, definition) ⇒ Object
説明文を更新
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 191 def update_definition!(term_name, definition) existing = load_terms.dup term = existing.find { it['term'] == term_name } return false unless term term['definition'] = definition term['updated_at'] = Time.now.strftime('%Y-%m-%d %H:%M:%S') save_terms!(existing) true end |
#update_flags!(term_name, new_flags) ⇒ Object
flags を更新
156 157 158 159 160 161 162 163 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 156 def update_flags!(term_name, new_flags) existing = load_terms.dup term = existing.find { it['term'] == term_name } return unless term term['flags'] = new_flags save_terms!(existing) end |
#update_yomi!(yomi_changes) ⇒ Object
読みを更新
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/vivlio/starter/cli/index/unified_terms_manager.rb', line 167 def update_yomi!(yomi_changes) return if yomi_changes.nil? || yomi_changes.empty? existing = load_terms.dup updated_count = 0 yomi_changes.each do |change| term = existing.find { it['term'] == change['term'] } next unless term next if term['yomi'] == change['yomi'] term['yomi'] = change['yomi'] updated_count += 1 end return unless updated_count.positive? save_terms!(existing) Common.log_info("#{updated_count} 件の読みを更新しました") end |