Sha256: 7d7d16252882f79bdd71c70dd6f7ff34854127cfaab9047eb4b801a8615c1a33

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

require 'delegate'

module Flipper
  module Adapters
    # Public: Adapter that wraps another adapter and stores the operations.
    #
    # Useful in tests to verify calls and such. Never use outside of testing.
    class OperationLogger < Wrapper

      class Operation
        attr_reader :type, :args, :kwargs

        def initialize(type, args, kwargs = {})
          @type = type
          @args = args
          @kwargs = kwargs
        end
      end

      # Internal: An array of the operations that have happened.
      attr_reader :operations

      # Public
      def initialize(adapter, operations = nil)
        super(adapter)
        @operations = operations || []
      end

      # Public: Count the number of times a certain operation happened.
      def count(type = nil)
        if type
          type(type).size
        else
          @operations.size
        end
      end

      # Public: Get all operations of a certain type.
      def type(type)
        @operations.select { |operation| operation.type == type }
      end

      # Public: Get the last operation of a certain type.
      def last(type)
        @operations.reverse.find { |operation| operation.type == type }
      end

      # Public: Resets the operation log to empty
      def reset
        @operations.clear
      end

      def inspect
        inspect_id = ::Kernel::format "%x", (object_id * 2)
        %(#<#{self.class}:0x#{inspect_id} @name=#{name.inspect}, @operations=#{@operations.inspect}, @adapter=#{@adapter.inspect}>)
      end

      private

      def wrap(method, *args, **kwargs, &block)
        @operations << Operation.new(method, args, kwargs)
        block.call
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
flipper-1.3.2 lib/flipper/adapters/operation_logger.rb
flipper-1.3.1 lib/flipper/adapters/operation_logger.rb
flipper-1.3.0 lib/flipper/adapters/operation_logger.rb
flipper-1.3.0.pre lib/flipper/adapters/operation_logger.rb