Module: Basquiat::Base

Defined in:
lib/basquiat/interfaces/base.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterBasquiat::Adapter

Deprecated.

event_adapter is deprecated and will be removed eventually. Please use #adapter.

Initializes and return a instance of the default adapter specified on Basquiat.configuration.default_adapter

Returns:

  • (Basquiat::Adapter)

    the adapter instance for the current class



32
33
34
# File 'lib/basquiat/interfaces/base.rb', line 32

def adapter=(adapter_klass)
  @adapter = @procs ? adapter_klass.new(procs: @procs) : adapter_klass.new
end

Class Method Details

.descendantsObject



13
14
15
# File 'lib/basquiat/interfaces/base.rb', line 13

def descendants
  @descendants ||= []
end

.extended(klass) ⇒ Object



9
10
11
# File 'lib/basquiat/interfaces/base.rb', line 9

def extended(klass)
  descendants.push klass
end

.reconfigure_childrenObject



17
18
19
# File 'lib/basquiat/interfaces/base.rb', line 17

def reconfigure_children
  descendants.each(&:reload_adapter_from_configuration)
end

Instance Method Details

#adapter_options(opts = Basquiat.configuration.adapter_options) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: Basquiat.configuration.adapter_options)

    The adapter specific options. Defaults to Basquiat.configuration.adapter_options



43
44
45
# File 'lib/basquiat/interfaces/base.rb', line 43

def adapter_options(opts = Basquiat.configuration.adapter_options)
  adapter.adapter_options(opts)
end

#connected?truthy, falsey

Utility method to check connection status

Returns:

  • (truthy, falsey)


72
73
74
# File 'lib/basquiat/interfaces/base.rb', line 72

def connected?
  adapter.connected?
end

#disconnectObject

Note:

The adapter should reconnect automatically.

Utility method to force a disconnect from the message queue.



66
67
68
# File 'lib/basquiat/interfaces/base.rb', line 66

def disconnect
  adapter.disconnect
end

#listen(block: true, rescue_proc: Basquiat.configuration.rescue_proc) ⇒ Object

Starts the consumer loop

Parameters:

  • block (Boolean) (defaults to: true)

    If it should block the thread. The relevance of this is dictated by the adapter. Defaults to true.



79
80
81
# 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) ⇒ Object

Publishes the message of type event to the queue. Note that the message will be converted to a JSON

Parameters:

  • event (String)

    the event name

  • message (#to_json)

    Message to be JSONfied and sent to the Message Queue



50
51
52
# File 'lib/basquiat/interfaces/base.rb', line 50

def publish(event, message)
  adapter.publish(event, message)
end

#reload_adapter_from_configurationObject



22
23
24
25
26
# 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) ⇒ Object

Subscribe the event with the proc passed.

Parameters:

  • event_name (String)

    the event name

  • 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.



59
60
61
62
# 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