class AmaLayout::Notification

Constants

DEFAULT_LIFESPAN
FORMAT_VERSION
TYPES

Attributes

active[RW]
brand[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

content[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

created_at[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

header[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

id[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

lifespan[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

type[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

version[R]

NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.

Public Class Methods

new(args = {}) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# File lib/ama_layout/notification.rb, line 27
def <=>(other)
  created_at <=> other.created_at
end
active?() click to toggle source
# File lib/ama_layout/notification.rb, line 31
def active?
  active
end
digest() click to toggle source
# File lib/ama_layout/notification.rb, line 44
def digest
  Digest::SHA256.hexdigest(
    "#{type}#{header}#{content}#{brand}#{version}"
  )
end
dismiss!() click to toggle source
# File lib/ama_layout/notification.rb, line 39
def dismiss!
  self.active = false
  dismissed?
end
dismissed?() click to toggle source
# File lib/ama_layout/notification.rb, line 35
def dismissed?
  !active?
end
stale?() click to toggle source
# File lib/ama_layout/notification.rb, line 50
def stale?
  Time.current > created_at + lifespan
end
to_h() click to toggle source
# 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

Private Instance Methods

invalid_type!() click to toggle source
# File lib/ama_layout/notification.rb, line 71
def invalid_type!
  raise ArgumentError, "invalid notification type: #{type}"
end
parse_duration(duration) click to toggle source
# File lib/ama_layout/notification.rb, line 79
def parse_duration(duration)
  if duration.is_a?(ActiveSupport::Duration)
    duration
  else
    duration.seconds
  end
end
parse_time(time) click to toggle source
# File lib/ama_layout/notification.rb, line 75
def parse_time(time)
  time.is_a?(String) ? Time.zone.parse(time) : time
end