class AmaLayout::Notification
Constants
- DEFAULT_LIFESPAN
- FORMAT_VERSION
- TYPES
Attributes
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
NOTE: The following attributes are designed to be immutable - you need make a new instance to change them. The only mutable attribute is :active.
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
# 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
# File lib/ama_layout/notification.rb, line 27 def <=>(other) created_at <=> other.created_at end
# File lib/ama_layout/notification.rb, line 31 def active? active end
# File lib/ama_layout/notification.rb, line 44 def digest Digest::SHA256.hexdigest( "#{type}#{header}#{content}#{brand}#{version}" ) end
# File lib/ama_layout/notification.rb, line 39 def dismiss! self.active = false dismissed? end
# File lib/ama_layout/notification.rb, line 35 def dismissed? !active? end
# File lib/ama_layout/notification.rb, line 50 def stale? Time.current > created_at + lifespan end
# 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
# File lib/ama_layout/notification.rb, line 71 def invalid_type! raise ArgumentError, "invalid notification type: #{type}" end
# File lib/ama_layout/notification.rb, line 79 def parse_duration(duration) if duration.is_a?(ActiveSupport::Duration) duration else duration.seconds end end
# File lib/ama_layout/notification.rb, line 75 def parse_time(time) time.is_a?(String) ? Time.zone.parse(time) : time end