class RB::Runner

Attributes

command[R]
options[R]

Public Class Methods

new(options) click to toggle source
# File lib/rb/runner.rb, line 6
def initialize(options)
  @options = options
end

Public Instance Methods

run(command) click to toggle source
# File lib/rb/runner.rb, line 10
def run(command)
  @command = command

  case true
  when options[:filter]
    filter
  when options[:apply]
    STDIN.tty? ? apply_to_tty : apply_to_stdin
  else
    puts eval(command)
  end
end

Private Instance Methods

apply_to_stdin() click to toggle source
# File lib/rb/runner.rb, line 43
def apply_to_stdin
  STDIN.each_line do |line|
    line = process_input(line)
    puts process_output(eval(command))
  end
end
apply_to_tty() click to toggle source
# File lib/rb/runner.rb, line 36
def apply_to_tty
  while line = STDIN.gets do
    line = process_input(line)
    puts "=> #{eval(command)}"
  end
end
filter() click to toggle source
# File lib/rb/runner.rb, line 27
def filter
  STDIN.select do |line|
    line = process_input(line)
    eval(command)
  end.each do |line|
    puts process_output(line)
  end
end
process_input(line) click to toggle source
# File lib/rb/runner.rb, line 50
def process_input(line)
  line.strip!
  line = JSON.load(line) if options[:json_input]
  line
end
process_output(line) click to toggle source
# File lib/rb/runner.rb, line 56
def process_output(line)
  line = JSON.dump(line) if options[:json_output]
  line
end