Class: RssNotifier::Db

Inherits:
Object
  • Object
show all
Defined in:
lib/rss_notifier/db.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Db

Returns a new instance of Db.



10
11
12
13
14
# File 'lib/rss_notifier/db.rb', line 10

def initialize(store)
  # { item_url => Item, ... }
  @items = {}
  @store = store
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



7
8
9
# File 'lib/rss_notifier/db.rb', line 7

def items
  @items
end

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/rss_notifier/db.rb', line 6

def store
  @store
end

Class Method Details

.load(filename) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rss_notifier/db.rb', line 59

def self.load(filename)
  store = YAML::Store.new(filename)
  db = RssNotifier::Db.new(store)
  store.transaction do
    db.items = store['items'] || {}
  end
  db
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rss_notifier/db.rb', line 32

def changed?
  0 < changed_items.size
end

#changed_itemsArray

Returns of Item.

Returns:

  • (Array)

    of Item



37
38
39
40
41
42
43
44
45
# File 'lib/rss_notifier/db.rb', line 37

def changed_items
  ch_items = []
  @items.each do |url, item|
    if item.new_record? || item.changed?
      ch_items << item
    end
  end
  ch_items
end

#saveObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rss_notifier/db.rb', line 47

def save
  return true unless changed?

  @items.each do |url, item|
    item.saved_at = Time.now
  end
  store.transaction do
    store['items'] = @items
  end
  true
end

#update(new_items) ⇒ Boolean

Returns changed?.

Parameters:

  • url (String)
  • items (Array)

    of Item

Returns:

  • (Boolean)

    changed?



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rss_notifier/db.rb', line 19

def update(new_items)
  new_items.each do |item|
    if !item.link || item.link.empty?
      RssNotifier.logger.warn "Empty item_url for url #{item.link}"
      next
    end

    old_item = self.items[item.link] ||= Item.new
    old_item.update(item.to_h)
  end
  changed?
end