Sha256: 1ba7a983d301a679b034dfafdc67f41c88d0179a6c118ed9b08da57e4c2bb559

Contents?: true

Size: 798 Bytes

Versions: 1

Compression:

Stored size: 798 Bytes

Contents

require "measure_its/version"

module MeasureIts
  class << self
    def add_strategy(name, &block)
      strategies[name] = block
    end

    def build(names)
      original = nil
      ss = names.map { |n| strategies.fetch(n) }
      new_method = ss.reverse_each.inject(lambda { original.call }) do |acc, s|
        lambda { s.call(&acc) }
      end
      lambda do |o|
        original = o
        new_method.call
      end
    end

    private

    def strategies
      @strategies ||= {}
    end
  end

  def measure_its(m, with:)
    m = Module.new do
      with_measure = MeasureIts.build(with)
      define_method(m) do |*args, &blk|
        with_measure.call(lambda { super(*args, &blk) })
      end
    end
    self.prepend(m)
  end

  refine(Module) do
    include MeasureIts
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
measure_its-0.1.0 lib/measure_its.rb