Sha256: 5a5c48977c2ba220a56a19cabf77fb75d2075fd1af408d10dd9d81a8e1062de0

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require_relative "dsl"

module Linepipe
  class Process
    include DSL
    attr_reader :output, :steps

    def initialize(io=STDOUT)
      @data         = []
      @steps        = []
      @setup        = nil
      @output       = nil
      @expectations = []
      @io           = io
    end

    def [](name)
      steps.detect { |s| s.name == name }
    end

    def run
      run_setup
      @output = steps.reduce(initial_data) { |d, step| step.apply(d) }
    end

    def develop
      run_setup
      @output = steps.to_enum.with_index.reduce(initial_data) { |d, (step, idx)|
        log "Stage #{idx}", step.name
        log "Input", d
        step.apply(d).tap do |r|
          log "Output", r
        end
      }

      if expectations.all? { |exp| exp.successful?(output) }
        log "Expect", "SUCCESS"
      end
    end

    def benchmark(iterations)
      require 'benchmark'
      require 'stringio'

      run_setup

      label_length = steps.map(&:name).map(&:length).max

      out = $stdout
      $stdout = stringio = StringIO.new

      Benchmark.bmbm(label_length) do |x|
        @output = steps.reduce(initial_data) { |d, step|
          result = step.apply(d)
          x.report(step.name) { iterations.times { step.apply(d) } }
          result
        }
      end

      io.puts stringio.string
    ensure
      $stdout = out
    end


    private
    attr_reader :expectations, :io

    def log(topic, msg = "")
      io.puts "\n[Linepipe] #{topic} #{msg}\n"
    end

    def run_setup
      @setup.call if @setup
    end

    def initial_data
      @data.call.tap do |data|
        if data.nil?
          log("Warn", "You need to specify an initial data set")
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linepipe-0.1.2 lib/linepipe/process.rb