Sha256: e5734381683af39090be2a4d13a1a935cc64578f557f389de5297714d7df0a73

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

require 'dh_easy/core/mock/fake_db'
require 'dh_easy/core/mock/fake_executor'
require 'dh_easy/core/mock/fake_parser'
require 'dh_easy/core/mock/fake_seeder'
require 'dh_easy/core/mock/fake_finisher'

module DhEasy
  module Core
    module Mock
      # Generate a context and message queue from a list of exposed methods.
      #
      # @param [Array] exposed_methods List of exposed methods.
      #
      # @example
      #   exposed_methods = [:boo, :bar]
      #   context, message_queue = DhEasy::Core::Mock.context_vars exposed_methods
      #   context.boo 1, 2
      #   context.bar 'A', 'B'
      #   context.bar '111', '222'
      #   message_queue
      #   # => [
      #   #   [:boo, [1, 2]],
      #   #   [:bar, ['A', 'B']],
      #   #   [:bar, ['111', '222']]
      #   # ]
      #
      # @return [Array] `[context, message_queue]` being:
      #   * `context`: Object implementing exposed methods.
      #   * `[Array] message_queue`: Array to store messages.
      def self.context_vars exposed_methods
        context = Object.new
        metaclass = class << context; self; end
        message_queue = [] # Beat reference bug
        exposed_methods = exposed_methods
        exposed_methods.each do |key|
          metaclass.send(:define_method, key) do |*args|
            # Record all method calls into message queue for easy access
            message_queue << [key, args]
          end
        end
        [context, message_queue]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dh_easy-core-0.3.4 lib/dh_easy/core/mock.rb
dh_easy-core-0.3.3 lib/dh_easy/core/mock.rb
dh_easy-core-0.3.2 lib/dh_easy/core/mock.rb
dh_easy-core-0.3.1 lib/dh_easy/core/mock.rb
dh_easy-core-0.2.2 lib/dh_easy/core/mock.rb