module Basquiat::Base

Base module used to extend the classes so that they will be able to use the event infrastructure

Public Class Methods

descendants() click to toggle source
# File lib/basquiat/interfaces/base.rb, line 13
def descendants
  @descendants ||= []
end
extended(klass) click to toggle source
# File lib/basquiat/interfaces/base.rb, line 9
def extended(klass)
  descendants.push klass
end
reconfigure_children() click to toggle source
# File lib/basquiat/interfaces/base.rb, line 17
def reconfigure_children
  descendants.each(&:reload_adapter_from_configuration)
end

Public Instance Methods

adapter() click to toggle source
# File lib/basquiat/interfaces/base.rb, line 38
def adapter
  @adapter ||= Kernel.const_get(Basquiat.configuration.default_adapter).new
end
adapter=(adapter_klass) click to toggle source

@!attribute [rw] adapter

Initializes and return a instance of the default adapter specified on Basquiat.configuration.default_adapter
@return [Basquiat::Adapter] the adapter instance for the current class
@deprecated event_adapter is deprecated and will be removed eventually. Please use {#adapter}.
# File lib/basquiat/interfaces/base.rb, line 32
def adapter=(adapter_klass)
  @adapter = @procs ? adapter_klass.new(procs: @procs) : adapter_klass.new
end
Also aliased as: event_adapter=
adapter_options(opts = Basquiat.configuration.adapter_options) click to toggle source

@param opts [Hash] The adapter specific options. Defaults to Basquiat.configuration.adapter_options

# File lib/basquiat/interfaces/base.rb, line 43
def adapter_options(opts = Basquiat.configuration.adapter_options)
  adapter.adapter_options(opts)
end
connected?() click to toggle source

Utility method to check connection status @return [truthy, falsey]

# File lib/basquiat/interfaces/base.rb, line 72
def connected?
  adapter.connected?
end
disconnect() click to toggle source

Utility method to force a disconnect from the message queue. @note The adapter should reconnect automatically.

# File lib/basquiat/interfaces/base.rb, line 66
def disconnect
  adapter.disconnect
end
event_adapter=(adapter_klass)
Alias for: adapter=
listen(block: true, rescue_proc: Basquiat.configuration.rescue_proc) click to toggle source

Starts the consumer loop @param block [Boolean] If it should block the thread. The relevance of this is dictated by the adapter.

Defaults to true.
# File lib/basquiat/interfaces/base.rb, line 79
def listen(block: true, rescue_proc: Basquiat.configuration.rescue_proc)
  adapter.listen(block: block, rescue_proc: rescue_proc)
end
publish(event, message) click to toggle source

Publishes the message of type event to the queue. Note that the message will be converted to a JSON @param event [String] the event name @param message [#to_json] Message to be JSONfied and sent to the Message Queue

# File lib/basquiat/interfaces/base.rb, line 50
def publish(event, message)
  adapter.publish(event, message)
end
reload_adapter_from_configuration() click to toggle source
# File lib/basquiat/interfaces/base.rb, line 22
def reload_adapter_from_configuration
  @procs = adapter.procs
  self.adapter = Kernel.const_get(Basquiat.configuration.default_adapter)
  adapter_options Basquiat.configuration.adapter_options
end
subscribe_to(event_name, proc) click to toggle source

Subscribe the event with the proc passed. @param event_name [String] the event name @param proc [Symbol, call] the proc to be executed when the event is consumed.

You can pass anything that answers to call or a symbol.
If a symbol is passed it will try to look for a public class method of the same name.
# File lib/basquiat/interfaces/base.rb, line 59
def subscribe_to(event_name, proc)
  proc = make_callable(proc)
  adapter.subscribe_to(event_name, proc)
end

Private Instance Methods

make_callable(proc) click to toggle source
# File lib/basquiat/interfaces/base.rb, line 85
def make_callable(proc)
  return proc if proc.respond_to? :call
  method(proc)
end