Class: AmaLayout::NotificationSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ama_layout/notification_set.rb

Overview

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"
}

}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_store, key) ⇒ NotificationSet

Returns a new instance of NotificationSet.



26
27
28
29
30
31
# 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

Instance Attribute Details

#baseObject

Returns the value of attribute base.



22
23
24
# File 'lib/ama_layout/notification_set.rb', line 22

def base
  @base
end

#data_storeObject

Returns the value of attribute data_store.



22
23
24
# File 'lib/ama_layout/notification_set.rb', line 22

def data_store
  @data_store
end

#keyObject

Returns the value of attribute key.



22
23
24
# File 'lib/ama_layout/notification_set.rb', line 22

def key
  @key
end

Instance Method Details

#activeObject



33
34
35
# File 'lib/ama_layout/notification_set.rb', line 33

def active
  all.select(&:active?)
end

#allObject



37
38
39
# File 'lib/ama_layout/notification_set.rb', line 37

def all
  @all ||= normalize(base_notifications)
end

#create(args = {}) ⇒ Object



41
42
43
44
45
46
47
48
# 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) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ama_layout/notification_set.rb', line 54

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!Object



50
51
52
# File 'lib/ama_layout/notification_set.rb', line 50

def destroy!
  data_store.delete(key) && reload!
end

#find(digest) ⇒ Object



63
64
65
# File 'lib/ama_layout/notification_set.rb', line 63

def find(digest)
  all.find { |n| n.id == digest }
end

#inspectObject Also known as: to_s



76
77
78
# File 'lib/ama_layout/notification_set.rb', line 76

def inspect
  "<#{self.class.name}>: #{all}"
end

#saveObject



67
68
69
70
71
72
73
74
# File 'lib/ama_layout/notification_set.rb', line 67

def save
  data_store.transaction do |store|
    normalized = normalize(all)
    self.base = serialize(normalized)
    store.set(key, base.to_json)
  end
  reload!
end