Class: Flyer::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/flyer/notification.rb

Constant Summary collapse

@@notifications =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Notification

Returns a new instance of Notification.



26
27
28
29
30
# File 'lib/flyer/notification.rb', line 26

def initialize(controller)
  @on = []
  @limit = 1
  @controller = controller
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/flyer/notification.rb', line 4

def id
  @id
end

#limitObject

Returns the value of attribute limit.



4
5
6
# File 'lib/flyer/notification.rb', line 4

def limit
  @limit
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/flyer/notification.rb', line 4

def params
  @params
end

#validObject

Returns the value of attribute valid.



4
5
6
# File 'lib/flyer/notification.rb', line 4

def valid
  @valid
end

Class Method Details

.init(&block) ⇒ Object



8
9
10
# File 'lib/flyer/notification.rb', line 8

def self.init(&block)
  @@notifications << block
end

.notificationsObject



19
20
21
# File 'lib/flyer/notification.rb', line 19

def self.notifications
  @@notifications
end

.reset!Object



12
13
14
# File 'lib/flyer/notification.rb', line 12

def self.reset!
  @@notifications = []
end

Instance Method Details

#expired?Boolean

Returns Boolean Has the notification expired?.

Returns:

  • (Boolean)

    Boolean Has the notification expired?



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/flyer/notification.rb', line 50

def expired?
  return false unless @valid

  if @valid[:to]
    if Date.parse(@valid.fetch(:to)) < Date.today
      return true
    end
  end

  if @valid[:from]
    if Date.parse(@valid.fetch(:from)) > Date.today
      return true
    end
  end
end

#message(&block) ⇒ Object



93
94
95
# File 'lib/flyer/notification.rb', line 93

def message(&block)
  @message = block
end

#on(&block) ⇒ Object



100
101
102
# File 'lib/flyer/notification.rb', line 100

def on(&block)
  @on << block
end

#path(&block) ⇒ Object



86
87
88
# File 'lib/flyer/notification.rb', line 86

def path(&block)
  @path = block
end

#used!Object

Mark current notification has used



43
44
45
# File 'lib/flyer/notification.rb', line 43

def used!
  cookies.permanent[token] = current_count + 1
end

#validate!Object

Raise an error if @id or @message is missing



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

def validate!
  raise Flyer::IdMissingError.new unless @id
  raise Flyer::MessageMissingError.new unless @message
end

#viewObject

Returns Flyer::ViewObject Object to be passed to view.

Returns:

  • Flyer::ViewObject Object to be passed to view



79
80
81
# File 'lib/flyer/notification.rb', line 79

def view
  Flyer::ViewObject.new(@controller, @path, @message, @params, @id)
end

#visible?Boolean

Returns Boolean Should the notification be visible?.

Returns:

  • (Boolean)

    Boolean Should the notification be visible?



69
70
71
72
73
74
# File 'lib/flyer/notification.rb', line 69

def visible?
  return false if hit_limit?
  return false if expired?
  return true if @on.empty?
  @on.any?{ |blk| @controller.instance_eval(&blk) }
end