Class: ReplRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/repl_runner.rb,
lib/repl_runner/config.rb,
lib/repl_runner/version.rb,
lib/repl_runner/pty_party.rb,
lib/repl_runner/multi_repl.rb,
lib/repl_runner/multi_command_parser.rb

Defined Under Namespace

Classes: Config, MultiCommandParser, MultiRepl, NoResultsError, PtyParty, UnregisteredCommand

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_type, command = nil, options = {}) ⇒ ReplRunner

Returns a new instance of ReplRunner.



26
27
28
29
30
31
32
33
34
35
# File 'lib/repl_runner.rb', line 26

def initialize(cmd_type, command = nil, options = {})
  raise "Unexpected block, use the `run` command instead?" if block_given?
  command  = cmd_type.to_s if command.nil?
  cmd_type = cmd_type.chomp.gsub(/\s/, '_').to_sym if cmd_type.is_a?(String)
  @command = command
  @repl    = nil
  @config  = get_config_for_command(cmd_type)
  raise UnregisteredCommand.new(cmd_type) unless @config
  @options = options
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/repl_runner.rb', line 9

def command
  @command
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/repl_runner.rb', line 9

def config
  @config
end

#replObject

Returns the value of attribute repl.



9
10
11
# File 'lib/repl_runner.rb', line 9

def repl
  @repl
end

Class Method Details

.known_configsObject



49
50
51
# File 'lib/repl_runner.rb', line 49

def known_configs
  @known_configs ||= {}
end

.register_command(*commands) {|config| ... } ⇒ Object Also known as: register_commands

Yields:



53
54
55
56
57
58
59
# File 'lib/repl_runner.rb', line 53

def register_command(*commands, &block)
  config = Config.new(commands)
  commands.each do |command|
    known_configs[command] = config
  end
  yield config
end

Instance Method Details

#closeObject



67
68
69
# File 'lib/repl_runner.rb', line 67

def close
  repl.close
end

#get_config_for_command(cmd_type) ⇒ Object



37
38
39
40
41
42
# File 'lib/repl_runner.rb', line 37

def get_config_for_command(cmd_type)
  known_configs.detect do |cmd_match, config|
    return config if cmd_match == cmd_type
    return config if cmd_match.is_a?(Regexp) && cmd_match =~ cmd_type.to_s
  end
end

#known_configsObject



44
45
46
# File 'lib/repl_runner.rb', line 44

def known_configs
  self.class.known_configs
end

#run(&block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/repl_runner.rb', line 82

def run(&block)
  repl = start
  yield repl
  repl.execute
ensure
  close if repl
end

#startObject



63
64
65
# File 'lib/repl_runner.rb', line 63

def start
  @repl = MultiRepl.new(command, @config.to_options.merge(@options))
end

#zip(string) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/repl_runner.rb', line 71

def zip(string)
  results = []
  inputs  = string.lines.map(&:rstrip)
  self.run do |repl|
    inputs.each do |line|
      repl.run(line) {|result| results << result }
    end
  end
  inputs.zip(results)
end