Module: Adalog::MockLoggingAdapter

Defined in:
lib/adalog/mock_logging_adapter.rb

Defined Under Namespace

Classes: Base, ReceivedMessage

Class Method Summary collapse

Class Method Details

.new(service_name, repo, **stub_methods) ⇒ Object

A factory to make new classes which implement the MockLogging



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/adalog/mock_logging_adapter.rb', line 6

def self.new(service_name, repo, **stub_methods)
  new_logger_class = Class.new(self::Base)
  new_logger_class.instance_variable_set(:@service_name, service_name)
  new_logger_class.instance_variable_set(:@repo, repo)

  stub_methods.each do |message, value|
    new_logger_class.instance_exec do
      define_method(message, &MockLoggingAdapter.stub_method(message, value))
    end
  end

  new_logger_class
end

.stub_method(message, value) ⇒ Object

An isolated lambda to serve as the method body for stubs, both in the ::new method and in the Base#initialize method. Avoids repeating already clunky-looking logic.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/adalog/mock_logging_adapter.rb', line 24

def self.stub_method(message, value)
  ->(*args, **kwargs, &block) {
    _received_messages << ReceivedMessage.new(message, args, kwargs, block)
    repo.insert(
      title:    service_name,
      message:  "'#{message}', which has been stubbed with '#{value}'.",
      details:  args)
    block.call unless nil == block
    value
  }
end