Class: AmaLayout::Notification
- Inherits:
-
Object
- Object
- AmaLayout::Notification
- Defined in:
- lib/ama_layout/notification.rb
Constant Summary collapse
- TYPES =
%i[notice warning alert].freeze
- DEFAULT_LIFESPAN =
1.year.freeze
- FORMAT_VERSION =
'1.0.0'.freeze
Instance Attribute Summary collapse
-
#active ⇒ Object
Returns the value of attribute active.
-
#brand ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#content ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#created_at ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#header ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#id ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#lifespan ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#type ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
-
#version ⇒ Object
readonly
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #active? ⇒ Boolean
- #digest ⇒ Object
- #dismiss! ⇒ Object
- #dismissed? ⇒ Boolean
-
#initialize(args = {}) ⇒ Notification
constructor
A new instance of Notification.
- #stale? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Notification
Returns a new instance of Notification.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ama_layout/notification.rb', line 13 def initialize(args = {}) args = args.with_indifferent_access @id = args[:id] @type = args.fetch(:type, :notice).to_sym @brand = args[:brand] @header = args.fetch(:header) @content = args.fetch(:content) @created_at = parse_time(args.fetch(:created_at)) @lifespan = parse_duration(args.fetch(:lifespan, DEFAULT_LIFESPAN)) @version = args.fetch(:version, FORMAT_VERSION) self.active = args.fetch(:active) invalid_type! if TYPES.exclude?(type) end |
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active.
11 12 13 |
# File 'lib/ama_layout/notification.rb', line 11 def active @active end |
#brand ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def brand @brand end |
#content ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def content @content end |
#created_at ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def created_at @created_at end |
#header ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def header @header end |
#id ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def id @id end |
#lifespan ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def lifespan @lifespan end |
#type ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def type @type end |
#version ⇒ Object (readonly)
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
9 10 11 |
# File 'lib/ama_layout/notification.rb', line 9 def version @version end |
Instance Method Details
#<=>(other) ⇒ Object
27 28 29 |
# File 'lib/ama_layout/notification.rb', line 27 def <=>(other) created_at <=> other.created_at end |
#active? ⇒ Boolean
31 32 33 |
# File 'lib/ama_layout/notification.rb', line 31 def active? active end |
#digest ⇒ Object
44 45 46 47 48 |
# File 'lib/ama_layout/notification.rb', line 44 def digest Digest::SHA256.hexdigest( "#{type}#{header}#{content}#{brand}#{version}" ) end |
#dismiss! ⇒ Object
39 40 41 42 |
# File 'lib/ama_layout/notification.rb', line 39 def dismiss! self.active = false dismissed? end |
#dismissed? ⇒ Boolean
35 36 37 |
# File 'lib/ama_layout/notification.rb', line 35 def dismissed? !active? end |
#stale? ⇒ Boolean
50 51 52 |
# File 'lib/ama_layout/notification.rb', line 50 def stale? Time.current > created_at + lifespan end |
#to_h ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ama_layout/notification.rb', line 54 def to_h # NOTE: We want the following keys to be strings to provide # consistency with the underlying data store. { 'type' => type.to_s, 'brand' => brand, 'header' => header, 'content' => content, 'created_at' => created_at.iso8601, 'active' => active, 'lifespan' => lifespan.to_i, 'version' => version } end |