Class: Basquiat::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/basquiat/adapters/base_adapter.rb

Overview

Base implementation for an adapter in uses HashRefinements internally.

Direct Known Subclasses

RabbitMq, Test

Instance Attribute Summary collapse

Adapter specific implementations collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(procs: {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • procs (Object) (defaults to: {})

    It’s a hash by default, but usually will be superseded by the adapter implementation



39
40
41
42
43
# File 'lib/basquiat/adapters/base_adapter.rb', line 39

def initialize(procs: {})
  @options = base_options
  @procs   = procs
  @retries = 0
end

Instance Attribute Details

#procsObject (readonly)

Returns the value of attribute procs.



96
97
98
# File 'lib/basquiat/adapters/base_adapter.rb', line 96

def procs
  @procs
end

Class Method Details

.register_strategy(config_name, klass) ⇒ Object

Used to register a requeue/acknowledge strategy

Parameters:

  • config_name (#to_sym)

    the named used on the config file for the Requeue Strategy

  • klass (Class)

    the class name.



22
23
24
# File 'lib/basquiat/adapters/base_adapter.rb', line 22

def register_strategy(config_name, klass)
  strategies[config_name.to_sym] = klass
end

.strategiesHash

A hash representing the registered requeue/acknowledge strategies

Returns:

  • (Hash)

    the registered RequeueStrategies



15
16
17
# File 'lib/basquiat/adapters/base_adapter.rb', line 15

def strategies
  @strategies ||= {}
end

.strategy(key) ⇒ Class

Return the Strategy Class registered on given key

Parameters:

  • key (#to_sym)

    configured key for the wanted strategy

Returns:

  • (Class)

    the strategy class

Raises:



30
31
32
33
34
# File 'lib/basquiat/adapters/base_adapter.rb', line 30

def strategy(key)
  strategies.fetch(key)
rescue KeyError
  raise Basquiat::Errors::StrategyNotRegistered
end

Instance Method Details

#adapter_options(opts) ⇒ Object

Allows the #base_options to be superseded on the local level

You could have configured an exchange_name (on a config file) to ‘awesome.sauce’, but on this object you’d want to publish your messages to the ‘killer.mailman’ exchange.

Examples:

class Mailmail
  extend Basquiat::Base
  adapter_options {exchange: {name: 'killer.mailman'}}
end

Parameters:

  • opts (Hash)

    an adapter dependant hash of options



61
62
63
# File 'lib/basquiat/adapters/base_adapter.rb', line 61

def adapter_options(opts)
  @options.deep_merge(opts)
end

#base_optionsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

rename this method

The default adapter options, merged with the Configuration#adapter_options. Used internally.

Returns:

  • (Hash)

    the full options hash



69
70
71
# File 'lib/basquiat/adapters/base_adapter.rb', line 69

def base_options
  default_options.merge(Basquiat.configuration.adapter_options)
end

#default_optionsHash

The adapter default options

Returns:

  • (Hash)


75
76
77
# File 'lib/basquiat/adapters/base_adapter.rb', line 75

def default_options
  {}
end

#disconnectObject

This method is abstract.

Disconnect from the message queue



91
92
93
# File 'lib/basquiat/adapters/base_adapter.rb', line 91

def disconnect
  raise Basquiat::Errors::SubclassResponsibility
end

#publishObject

This method is abstract.

Publish an event to the event stream



81
82
83
# File 'lib/basquiat/adapters/base_adapter.rb', line 81

def publish
  raise Basquiat::Errors::SubclassResponsibility
end

#strategiesObject

Utility method to access the class instance variable



46
47
48
# File 'lib/basquiat/adapters/base_adapter.rb', line 46

def strategies
  self.class.strategies
end

#subscribe_toObject

This method is abstract.

subscribe_to the event stream



86
87
88
# File 'lib/basquiat/adapters/base_adapter.rb', line 86

def subscribe_to
  raise Basquiat::Errors::SubclassResponsibility
end