Class: AmaLayout::Notification

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#activeObject

Returns the value of attribute active.



11
12
13
# File 'lib/ama_layout/notification.rb', line 11

def active
  @active
end

#brandObject (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

#contentObject (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_atObject (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

#headerObject (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

#idObject (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

#lifespanObject (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

#typeObject (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

#versionObject (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

Returns:

  • (Boolean)


31
32
33
# File 'lib/ama_layout/notification.rb', line 31

def active?
  active
end

#digestObject



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

Returns:

  • (Boolean)


35
36
37
# File 'lib/ama_layout/notification.rb', line 35

def dismissed?
  !active?
end

#stale?Boolean

Returns:

  • (Boolean)


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

def stale?
  Time.current > created_at + lifespan
end

#to_hObject



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