Class: Dubhe::Reader
- Inherits:
-
Object
- Object
- Dubhe::Reader
- Defined in:
- lib/dubhe/reader.rb,
sig/dubhe.rbs
Instance Attribute Summary collapse
-
#current_entry ⇒ Object
readonly
Returns the value of attribute current_entry.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Instance Method Summary collapse
- #add_source(source) ⇒ Object
- #back ⇒ Object
- #entries(source_id: nil, filter: nil) ⇒ Array[untyped]
- #forward ⇒ Object
-
#initialize(sources: [], store: Store.new) ⇒ Reader
constructor
A new instance of Reader.
- #mark_read(entry, read: true) ⇒ Object
- #open(entry) ⇒ Object
- #refresh ⇒ Array[untyped]
- #search(query, source_id: nil) ⇒ Array[untyped]
- #star(entry, starred: true) ⇒ Object
Constructor Details
Instance Attribute Details
#current_entry ⇒ Object (readonly)
Returns the value of attribute current_entry.
5 6 7 |
# File 'lib/dubhe/reader.rb', line 5 def current_entry @current_entry end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
5 6 7 |
# File 'lib/dubhe/reader.rb', line 5 def sources @sources end |
Instance Method Details
#add_source(source) ⇒ Object
15 16 17 18 |
# File 'lib/dubhe/reader.rb', line 15 def add_source(source) @sources << source source end |
#back ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/dubhe/reader.rb', line 44 def back return nil if @history_index <= 0 @history_index -= 1 @current_entry = @history[@history_index] body_for(@current_entry) end |
#entries(source_id: nil, filter: nil) ⇒ Array[untyped]
24 25 26 27 28 29 30 31 |
# File 'lib/dubhe/reader.rb', line 24 def entries(source_id: nil, filter: nil) result = if source_id @sources.find { |source| source.id == source_id }&.entries(filter: filter) || [] else @sources.flat_map { |source| source.entries(filter: filter) } end result.sort_by { |entry| entry.updated_at || entry.published_at || Time.at(0) }.reverse end |
#forward ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/dubhe/reader.rb', line 52 def forward return nil if @history_index >= @history.length - 1 @history_index += 1 @current_entry = @history[@history_index] body_for(@current_entry) end |
#mark_read(entry, read: true) ⇒ Object
60 61 62 63 |
# File 'lib/dubhe/reader.rb', line 60 def mark_read(entry, read: true) @store.update_state(entry.id, state: read ? :read : :unread) entry end |
#open(entry) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dubhe/reader.rb', line 33 def open(entry) source = @sources.find { |candidate| candidate.id == entry.source_id } raise Error, "source not found: #{entry.source_id}" unless source @current_entry = entry @history = @history[0..@history_index] if @history_index >= 0 @history << entry @history_index = @history.length - 1 source.fetch_body(entry) end |
#refresh ⇒ Array[untyped]
20 21 22 |
# File 'lib/dubhe/reader.rb', line 20 def refresh @sources.flat_map(&:refresh) end |
#search(query, source_id: nil) ⇒ Array[untyped]
70 71 72 73 74 75 76 |
# File 'lib/dubhe/reader.rb', line 70 def search(query, source_id: nil) needle = query.to_s.downcase entries(source_id: source_id).select do |entry| body = @store.body(entry.id)&.content.to_s [entry.title, entry., entry.summary, body].compact.join(" ").downcase.include?(needle) end end |
#star(entry, starred: true) ⇒ Object
65 66 67 68 |
# File 'lib/dubhe/reader.rb', line 65 def star(entry, starred: true) @store.update_state(entry.id, state: starred ? :starred : :read) entry end |