class AmaLayout::NotificationSet
An array-like object that handles the storage and retrieval of notifications from the underlying data store.
The raw serialization format is JSON as follows (keys are SHA256 hashes):
{
"57107043eab0f60a37f7735307dc6fc6709d04eec2dbeea8c284958057af9b77": { "type": "notice", "brand": "membership", "header": "test", "content": "test", "created_at": "2017-06-19T11:26:57.730-06:00", "lifespan": 31557600, "active": true, "version": "1.0.0" }
}
Attributes
base[RW]
data_store[RW]
key[RW]
Public Class Methods
new(data_store, key)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 26 def initialize(data_store, key) self.data_store = data_store self.key = key self.base = fetch clean! end
Public Instance Methods
active()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 33 def active all.select(&:active?) end
all()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 37 def all @all ||= normalize(base_notifications) end
create(args = {})
click to toggle source
# File lib/ama_layout/notification_set.rb, line 41 def create(args = {}) args[:created_at] = Time.current args[:active] = true notification = Notification.new(args) # previously dismissed notifications always take precendence all.push(notification) unless base.key?(notification.digest) save end
delete(*digests)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 55 def delete(*digests) digests = Array.wrap(digests.flatten) delta = all.reject { |n| digests.include?(n.digest) } if delta != all @all = delta save end end
destroy!()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 50 def destroy! data_store.delete(key) reload! end
find(digest)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 64 def find(digest) all.find { |n| n.id == digest } end
inspect()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 77 def inspect "<#{self.class.name}>: #{all}" end
Also aliased as: to_s
save()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 68 def save data_store.transaction do |store| normalized = normalize(all) self.base = serialize(normalized) store.set(key, base.to_json) end reload! end
Private Instance Methods
base_notifications()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 102 def base_notifications base.map { |k, v| Notification.new(v.merge(id: k)) } end
build(raw)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 123 def build(raw) JSON.parse(raw) rescue JSON::ParserError data_store.delete(key) # we should try to prevent further errors ::Rails.logger.error json_message(__FILE__, __LINE__, raw) {} end
clean!()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 84 def clean! if dirty? all.reject!(&:stale?) save end end
dirty?()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 91 def dirty? all.any?(&:stale?) end
fetch()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 118 def fetch result = data_store.get(key) result.present? ? build(result) : {} end
json_message(file, line, raw)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 131 def json_message(file, line, raw) { error: "#{self.class.name} - Invalid JSON", file: file, line: line, key: key, raw: raw }.to_json end
normalize(data)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 113 def normalize(data) # sort by reverse chronological order data.sort { |a, b| b <=> a } end
reload!()
click to toggle source
# File lib/ama_layout/notification_set.rb, line 95 def reload! @all = nil self.base = fetch all self end
serialize(data)
click to toggle source
# File lib/ama_layout/notification_set.rb, line 106 def serialize(data) data.inject({}) do |hash, element| hash[element.digest] = element.to_h hash end end