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

TODO Add support for multiple targets



53
54
55
56
57
# File 'lib/dallal/notification.rb', line 53

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

#notify(*args, &block) ⇒ Object

TODO Collection of targets is not supported



15
16
17
18
19
20
21
# File 'lib/dallal/notification.rb', line 15

def notify *args, &block
  notify_opts = args.extract_options!
  return unless should_send?(notify_opts[:if])

  @target = Array(args).flatten.compact.uniq
  instance_eval(&block)
end

#payload(payload) ⇒ Object

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



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

def payload payload
  @payload = payload
end

#persist?Boolean

Returns:

  • (Boolean)


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

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

#template(template) ⇒ Object

Same here as payload



44
45
46
# File 'lib/dallal/notification.rb', line 44

def template template
  @template_name = template
end

#userObject

TODO fix this. This is a quick and dirty patch for email



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

def user
  @target.first
end

#with(*args, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/dallal/notification.rb', line 23

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