class WithEvents::Event

Attributes

callback[R]
condition[R]
finder[R]
identifier[R]
klass[R]
name[R]
options[R]
stream[R]

Public Class Methods

new(name, klass, options = {}) click to toggle source

Options:

name - event name klass - resource class name options[:condition] - condition to check whether event can be triggered options[:callback] - callback to invoke on event options[:stream] - stream object event belongs to options[:identifier] - resource identifier (symbol, Proc or Class) options[:finder] - resource finder (symbol, Proc or Class) options[:subscribe] - subscribe to SQS queue

# File lib/with_events/event.rb, line 19
def initialize(name, klass, options = {})
  @name = name
  @klass = klass
  @options = options
  @condition = options[:condition]
  @callback = options[:callback]
  @stream = options[:stream]
  @identifier = options[:identifier]
  @finder = options[:finder]

  define_condition
  define_callback
end

Private Instance Methods

define_callback() click to toggle source
# File lib/with_events/event.rb, line 48
def define_callback
  klass.instance_exec(self) do |event|
    define_method("#{event.name}!") do
      event.stream.notify(event, self)
      return if event.stream.subscribe || !event.callback
      Invoker.new(event.callback).invoke(self)
    end
  end
end
define_condition() click to toggle source
# File lib/with_events/event.rb, line 37
def define_condition
  return unless condition

  klass.instance_exec(self) do |event|
    define_method("#{event.name}?") do
      return false unless event.condition
      Invoker.new(event.condition).invoke(self)
    end
  end
end