Class: VivlioStarter::CLI::IndexCommands::HierarchicalIndex
- Inherits:
-
Object
- Object
- VivlioStarter::CLI::IndexCommands::HierarchicalIndex
- Defined in:
- lib/vivlio_starter/cli/index/hierarchical_index.rb
Overview
階層化索引ビルダー
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#hierarchy ⇒ Object
readonly
Returns the value of attribute hierarchy.
Instance Method Summary collapse
-
#add_entry(term, link, parent: nil) ⇒ Object
エントリを追加.
-
#calculate_page_ranges(page_numbers) ⇒ Array<String>
ページ範囲を計算(連続ページをまとめる).
-
#children_of(term) ⇒ Object
用語の子用語を取得.
-
#deduplicate_same_page! ⇒ Object
同一ページの重複を排除 HTMLファイル名が同じリンクをまとめる.
-
#entry_count ⇒ Object
エントリ数を取得.
-
#get_hierarchy ⇒ Hash
階層構造を取得.
-
#initialize ⇒ HierarchicalIndex
constructor
A new instance of HierarchicalIndex.
-
#link_count ⇒ Object
リンク総数を取得.
-
#root_terms ⇒ Object
ルートレベルの用語を取得(親を持たない用語).
Constructor Details
#initialize ⇒ HierarchicalIndex
Returns a new instance of HierarchicalIndex.
32 33 34 35 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 32 def initialize @entries = Hash.new { |h, k| h[k] = [] } @hierarchy = Hash.new { |h, k| h[k] = Set[] } end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
30 31 32 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 30 def entries @entries end |
#hierarchy ⇒ Object (readonly)
Returns the value of attribute hierarchy.
30 31 32 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 30 def hierarchy @hierarchy end |
Instance Method Details
#add_entry(term, link, parent: nil) ⇒ Object
エントリを追加
41 42 43 44 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 41 def add_entry(term, link, parent: nil) @entries[term] << link @hierarchy[parent] << term if parent end |
#calculate_page_ranges(page_numbers) ⇒ Array<String>
ページ範囲を計算(連続ページをまとめる)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 61 def calculate_page_ranges(page_numbers) return [] if page_numbers.empty? sorted = page_numbers.uniq.sort ranges = [] range_start = sorted.first range_end = sorted.first sorted[1..].each do |num| if num == range_end + 1 range_end = num else ranges << format_range(range_start, range_end) range_start = num range_end = num end end ranges << format_range(range_start, range_end) ranges end |
#children_of(term) ⇒ Object
用語の子用語を取得
96 97 98 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 96 def children_of(term) @hierarchy[term].to_a end |
#deduplicate_same_page! ⇒ Object
同一ページの重複を排除 HTMLファイル名が同じリンクをまとめる
48 49 50 51 52 53 54 55 56 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 48 def deduplicate_same_page! @entries.each do |term, links| # ファイル名ごとにグループ化 grouped = links.group_by { |link| link.split('#').first } # 各ファイルの最初のリンクのみを保持 @entries[term] = grouped.map { |_, group| group.first } end end |
#entry_count ⇒ Object
エントリ数を取得
101 102 103 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 101 def entry_count @entries.size end |
#get_hierarchy ⇒ Hash
階層構造を取得
85 86 87 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 85 def get_hierarchy @hierarchy.transform_values(&:to_a) end |
#link_count ⇒ Object
リンク総数を取得
106 107 108 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 106 def link_count @entries.values.sum(&:size) end |
#root_terms ⇒ Object
ルートレベルの用語を取得(親を持たない用語)
90 91 92 93 |
# File 'lib/vivlio_starter/cli/index/hierarchical_index.rb', line 90 def root_terms all_children = @hierarchy.values.flatten.to_set @entries.keys.reject { all_children.include?(it) } end |