Class: Vivlio::Starter::CLI::IndexCommands::HierarchicalIndex
- Inherits:
-
Object
- Object
- Vivlio::Starter::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.
25 26 27 28 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 25 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.
23 24 25 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 23 def entries @entries end |
#hierarchy ⇒ Object (readonly)
Returns the value of attribute hierarchy.
23 24 25 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 23 def hierarchy @hierarchy end |
Instance Method Details
#add_entry(term, link, parent: nil) ⇒ Object
エントリを追加
34 35 36 37 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 34 def add_entry(term, link, parent: nil) @entries[term] << link @hierarchy[parent] << term if parent end |
#calculate_page_ranges(page_numbers) ⇒ Array<String>
ページ範囲を計算(連続ページをまとめる)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 54 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
用語の子用語を取得
89 90 91 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 89 def children_of(term) @hierarchy[term].to_a end |
#deduplicate_same_page! ⇒ Object
同一ページの重複を排除HTMLファイル名が同じリンクをまとめる
41 42 43 44 45 46 47 48 49 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 41 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
エントリ数を取得
94 95 96 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 94 def entry_count @entries.size end |
#get_hierarchy ⇒ Hash
階層構造を取得
78 79 80 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 78 def get_hierarchy @hierarchy.transform_values(&:to_a) end |
#link_count ⇒ Object
リンク総数を取得
99 100 101 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 99 def link_count @entries.values.sum(&:size) end |
#root_terms ⇒ Object
ルートレベルの用語を取得(親を持たない用語)
83 84 85 86 |
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 83 def root_terms all_children = @hierarchy.values.flatten.to_set @entries.keys.reject { all_children.include?(it) } end |