Class: Dubhe::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/dubhe/reader.rb,
sig/dubhe.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources: [], store: Store.new) ⇒ Reader

Returns a new instance of Reader.

Parameters:

  • sources: (Array[untyped]) (defaults to: [])
  • store: (Store) (defaults to: Store.new)


7
8
9
10
11
12
13
# File 'lib/dubhe/reader.rb', line 7

def initialize(sources: [], store: Store.new)
  @store = store
  @sources = Array(sources)
  @history = []
  @history_index = -1
  @current_entry = nil
end

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

Returns:

  • (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]

Parameters:

  • source_id: (String, nil) (defaults to: nil)
  • filter: (Proc, nil) (defaults to: nil)

Returns:

  • (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

Returns:

  • (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

Parameters:

  • entry (Object)

Returns:

  • (Object)

Raises:



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]

Returns:

  • (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]

Parameters:

  • query (String)
  • source_id: (String, nil) (defaults to: nil)

Returns:

  • (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.author, 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