Class: Jrf::CLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/jrf/cli/runner.rb

Defined Under Namespace

Classes: RsNormalizer

Constant Summary collapse

RS_CHAR =
"\x1e"
DEFAULT_OUTPUT_BUFFER_LIMIT =
4096

Instance Method Summary collapse

Constructor Details

#initialize(inputs:, out: $stdout, err: $stderr, lax: false, pretty: false, atomic_write_bytes: DEFAULT_OUTPUT_BUFFER_LIMIT) ⇒ Runner

Returns a new instance of Runner.



31
32
33
34
35
36
37
38
39
# File 'lib/jrf/cli/runner.rb', line 31

def initialize(inputs:, out: $stdout, err: $stderr, lax: false, pretty: false, atomic_write_bytes: DEFAULT_OUTPUT_BUFFER_LIMIT)
  @inputs = inputs
  @out = out
  @err = err
  @lax = lax
  @pretty = pretty
  @atomic_write_bytes = atomic_write_bytes
  @output_buffer = +""
end

Instance Method Details

#run(expression, verbose: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jrf/cli/runner.rb', line 41

def run(expression, verbose: false)
  parsed = PipelineParser.new(expression).parse
  stages = parsed[:stages]
  dump_stages(stages) if verbose

  blocks = stages.map { |stage|
    eval("proc { #{stage[:src]} }", nil, "(jrf stage)", 1) # rubocop:disable Security/Eval
  }
  pipeline = Pipeline.new(*blocks)

  input_enum = Enumerator.new { |y| each_input_value { |v| y << v } }
  pipeline.call(input_enum) do |value|
    emit_output(value)
  end
ensure
  write_output(@output_buffer)
end