module Adalog::StubLoggingAdapter

Public Class Methods

new(service_name, repo, **stub_methods) click to toggle source

A factory to make new classes which implement the MockLogging

# File lib/adalog/stub_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, &StubLoggingAdapter.stub_method(message, value))
    end
  end

  new_logger_class
end
stub_method(message, value) click to toggle source

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.

# File lib/adalog/stub_logging_adapter.rb, line 24
def self.stub_method(message, value)
  ->(*args, &block) {
    repo.insert(
      title:    service_name,
      message:  "'#{message}', which has been stubbed with '#{value}'.",
      details:  args)
    block.call unless nil == block
    value
  }
end