Sha256: ad4e5c0e4f20c07c8c58479f67858a28570b05acf2e8dd72f6c14fabd5391a4e

Contents?: true

Size: 1.24 KB

Versions: 16

Compression:

Stored size: 1.24 KB

Contents

require 'flipper/adapters/decorator'

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 < Decorator
      Operation = Struct.new(:type, :args)

      OperationTypes = [
        :features,
        :add,
        :remove,
        :clear,
        :get,
        :enable,
        :disable,
      ]

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

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

      # Wraps original method with in memory log of operations performed.
      OperationTypes.each do |type|
        class_eval <<-EOE
          def #{type}(*args)
            @operations << Operation.new(:#{type}, args)
            super
          end
        EOE
      end

      # Public: Count the number of times a certain operation happened.
      def count(type)
        @operations.select { |operation| operation.type == type }.size
      end

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
flipper-0.7.5 lib/flipper/adapters/operation_logger.rb
flipper-0.7.4 lib/flipper/adapters/operation_logger.rb
flipper-0.7.3 lib/flipper/adapters/operation_logger.rb
flipper-0.7.2 lib/flipper/adapters/operation_logger.rb
flipper-0.7.1 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0.beta6 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0.beta5 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0.beta4 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0.beta3 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0.beta2 lib/flipper/adapters/operation_logger.rb
flipper-0.7.0.beta1 lib/flipper/adapters/operation_logger.rb
flipper-0.6.3 lib/flipper/adapters/operation_logger.rb
flipper-0.6.2 lib/flipper/adapters/operation_logger.rb
flipper-0.6.1 lib/flipper/adapters/operation_logger.rb
flipper-0.6.0 lib/flipper/adapters/operation_logger.rb