Class: Cosensee::PageStore
- Inherits:
-
Object
- Object
- Cosensee::PageStore
- Extended by:
- Delegatable
- Defined in:
- lib/cosensee/page_store.rb
Overview
To search for pages
Constant Summary collapse
- DEFAULT_PIN_TITLES =
['HOME'].freeze
Instance Attribute Summary collapse
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#pin_titles ⇒ Object
readonly
Returns the value of attribute pin_titles.
Instance Method Summary collapse
- #create_title_index(pages) ⇒ Object
- #dump_search_data ⇒ Object
- #find_link_pages_by_title(title) ⇒ Object
- #find_page_by_title(title) ⇒ Object
-
#initialize(project:) ⇒ PageStore
constructor
A new instance of PageStore.
- #linked_pages ⇒ Object
- #linking_pages ⇒ Object
- #orphan_page_titles ⇒ Object
- #pages_by_title ⇒ Object
- #pinned_pages ⇒ Object
- #setup_link_indexes ⇒ Object
- #title_exist?(title) ⇒ Boolean
Methods included from Delegatable
Constructor Details
#initialize(project:) ⇒ PageStore
Returns a new instance of PageStore.
13 14 15 16 17 18 19 20 |
# File 'lib/cosensee/page_store.rb', line 13 def initialize(project:) @project = project @pages = project.pages @pages_by_title = nil @linking_pages = nil @linked_pages = nil @pin_titles = DEFAULT_PIN_TITLES.dup end |
Instance Attribute Details
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
22 23 24 |
# File 'lib/cosensee/page_store.rb', line 22 def pages @pages end |
#pin_titles ⇒ Object (readonly)
Returns the value of attribute pin_titles.
22 23 24 |
# File 'lib/cosensee/page_store.rb', line 22 def pin_titles @pin_titles end |
Instance Method Details
#create_title_index(pages) ⇒ Object
52 53 54 55 56 |
# File 'lib/cosensee/page_store.rb', line 52 def create_title_index(pages) pages.each_with_object({}) do |page, hash| hash[page.title] = page end end |
#dump_search_data ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/cosensee/page_store.rb', line 67 def dump_search_data pages.map do |page| { title: page.title, link: page.link_path, summary: page.summary_text } end end |
#find_link_pages_by_title(title) ⇒ Object
62 63 64 65 |
# File 'lib/cosensee/page_store.rb', line 62 def find_link_pages_by_title(title) pages = linking_pages[title] + linked_pages[title] pages.sort_by(&:updated).uniq.reverse end |
#find_page_by_title(title) ⇒ Object
28 29 30 |
# File 'lib/cosensee/page_store.rb', line 28 def find_page_by_title(title) pages_by_title[title] end |
#linked_pages ⇒ Object
38 39 40 41 42 |
# File 'lib/cosensee/page_store.rb', line 38 def linked_pages setup_link_indexes unless @linked_pages @linked_pages end |
#linking_pages ⇒ Object
32 33 34 35 36 |
# File 'lib/cosensee/page_store.rb', line 32 def linking_pages setup_link_indexes unless @linking_pages @linking_pages end |
#orphan_page_titles ⇒ Object
48 49 50 |
# File 'lib/cosensee/page_store.rb', line 48 def orphan_page_titles linked_pages.keys.reject { |title| title_exist?(title) } end |
#pages_by_title ⇒ Object
24 25 26 |
# File 'lib/cosensee/page_store.rb', line 24 def pages_by_title @pages_by_title ||= create_title_index(pages) end |
#pinned_pages ⇒ Object
58 59 60 |
# File 'lib/cosensee/page_store.rb', line 58 def pinned_pages pages_by_title.fetch_values(*pin_titles) { nil }.compact end |
#setup_link_indexes ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cosensee/page_store.rb', line 75 def setup_link_indexes @linking_pages = Hash.new { |h, k| h[k] = [] } @linked_pages = Hash.new { |h, k| h[k] = [] } @project.pages.each do |page| page.linking_page_titles.each do |linking_title| @linking_pages[page.title] << pages_by_title[linking_title] if pages_by_title[linking_title] @linked_pages[linking_title] << page end end end |
#title_exist?(title) ⇒ Boolean
44 45 46 |
# File 'lib/cosensee/page_store.rb', line 44 def title_exist?(title) !!pages_by_title[title] end |