Class: Dallal::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Notification

Returns a new instance of Notification.



6
7
8
9
10
11
12
# File 'lib/dallal/notification.rb', line 6

def initialize args = {}
  args.each do |k, v|
    send("#{k}=", v)
  end

  @notifiers = {}
end

Instance Attribute Details

#_objectObject

Returns the value of attribute _object.



3
4
5
# File 'lib/dallal/notification.rb', line 3

def _object
  @_object
end

#eventObject

Returns the value of attribute event.



3
4
5
# File 'lib/dallal/notification.rb', line 3

def event
  @event
end

#model_classObject

Returns the value of attribute model_class.



3
4
5
# File 'lib/dallal/notification.rb', line 3

def model_class
  @model_class
end

#optsObject

Returns the value of attribute opts.



3
4
5
# File 'lib/dallal/notification.rb', line 3

def opts
  @opts
end

#template_nameObject (readonly)

Returns the value of attribute template_name.



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

def template_name
  @template_name
end

Instance Method Details

#dispatch!Object



46
47
48
49
50
# File 'lib/dallal/notification.rb', line 46

def dispatch!
  validate!
  @notifiers.each { |_, n| n.notify! }
  @notifiers.each { |_, n| n.persist! } if persist?
end

#notify(target, &block) ⇒ Object



14
15
16
17
# File 'lib/dallal/notification.rb', line 14

def notify target, &block
  @target = Array(target).flatten.compact.uniq
  instance_eval(&block)
end

#payload(payload) ⇒ Object

TODO !!! Watch out same payload for multiple notifers that require payload



33
34
35
# File 'lib/dallal/notification.rb', line 33

def payload payload
  @payload = payload
end

#persist?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dallal/notification.rb', line 42

def persist?
  opts[:persist].present?
end

#template(template) ⇒ Object

Same here as payload



38
39
40
# File 'lib/dallal/notification.rb', line 38

def template template
  @template_name = template
end

#userObject



27
28
29
# File 'lib/dallal/notification.rb', line 27

def user
  @target.first
end

#with(*args, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/dallal/notification.rb', line 19

def with *args, &block
  opts = args.extract_options!
  if should_send?(opts[:if])
    instance_eval(&block)
    args.each { |arg| @notifiers[arg] = get_notifier(arg) }
  end
end