Class: RssNotifier::Db
- Inherits:
-
Object
- Object
- RssNotifier::Db
- Defined in:
- lib/rss_notifier/db.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Class Method Summary collapse
Instance Method Summary collapse
- #changed? ⇒ Boolean
-
#changed_items ⇒ Array
Of Item.
-
#initialize(store) ⇒ Db
constructor
A new instance of Db.
- #save ⇒ Object
-
#update(new_items) ⇒ Boolean
Changed?.
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
#items ⇒ Object
Returns the value of attribute items.
7 8 9 |
# File 'lib/rss_notifier/db.rb', line 7 def items @items end |
#store ⇒ Object (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
32 33 34 |
# File 'lib/rss_notifier/db.rb', line 32 def changed? 0 < changed_items.size end |
#changed_items ⇒ Array
Returns 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 |
#save ⇒ Object
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?.
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 |