class Dallal::Notification

Attributes

_object[RW]
event[RW]
model_class[RW]
notifiers[R]
opts[RW]
targets[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/dallal/notification.rb, line 10
def initialize args = {}
  args.each do |k, v|
    send("#{k}=", v)
  end

  @notifiers = []
end

Public Instance Methods

dispatch!() click to toggle source
# File lib/dallal/notification.rb, line 43
def dispatch!
  validate!
  @notifiers.each do |notifier|
    # notifier.validate!
    notifier.notify!
  end
  @notifiers.each { |n| n.persist! } if persist?
end
notify(*args, &block) click to toggle source
# File lib/dallal/notification.rb, line 18
def notify *args, &block
  notify_opts = args.extract_options!
  return unless should_send?(notify_opts[:if])

  @targets = Array(args).flatten.compact.uniq
  instance_eval(&block)
end
persist?() click to toggle source
# File lib/dallal/notification.rb, line 39
def persist?
  opts[:persist].present?
end
with(*args, &block) click to toggle source
# File lib/dallal/notification.rb, line 26
def with *args, &block
  opts = args.extract_options!
  return unless should_send?(opts[:if])

  # create a notifier for each target
  @targets.each do |target|
    args.each do |name|
      notifier = get_notifier(name, target, &block)
      @notifiers << notifier
    end
  end
end

Private Instance Methods

get_notifier(name, target, &block) click to toggle source
# File lib/dallal/notification.rb, line 61
def get_notifier(name, target, &block)
  notification = "Dallal::Notifications::#{name.to_s.camelcase}Notification".constantize.new(self, target)
  notification.instance_eval(&block)
  notification.notifier
end
should_send?(condition) click to toggle source
# File lib/dallal/notification.rb, line 67
def should_send?(condition)
  return true if condition.blank?

  if condition.is_a?(Proc)
    instance_exec(&condition)
  else
    _object.send(condition)
  end
end
validate!() click to toggle source

TODO Implement this Add validation logic here

# File lib/dallal/notification.rb, line 55
def validate!
  if false
    raise "You have not defined \'notify\' in \'with\' block for #{class_name} notifier"
  end
end