Sha256: 4c04488aaa08b58ba181b8ed360dc64600ca3c67103956a0146f9b3b6b990bbd

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module Support

  # Module for recording operations.
  #
  #   Support::Stats.install!
  #
  #   stats = Support::Stats.collect do
  #     session.with(write: :propagate)[:users].insert({})
  #   end
  #
  #   ops = stats["127.0.0.1:27017"]
  #   ops.size # => 2
  #   ops[0].class # => Moped::Protocol::Insert
  #   ops[1].class # => Moped::Protocol::Command
  #
  module Stats
    extend self

    def record(node, operations)
      key = if node.primary?
        :primary
      elsif node.secondary?
        :secondary
      else
        :other
      end

      @stats[key].concat(operations) if @stats
    end

    def collect
      @stats = Hash.new { |hash, key| hash[key] = [] }
      yield
      @stats
    ensure
      @stats = nil
    end

    def install!
      Moped::Node.class_eval <<-EOS
        alias _logging logging

        def logging(operations, &block)
          Support::Stats.record(self, operations)
          _logging(operations, &block)
        end
      EOS
      @stats = nil
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/bundler/gems/moped-cf817ca58a85/spec/support/stats.rb