Class: ReplRunner::MultiCommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/repl_runner/multi_command_parser.rb

Constant Summary collapse

STRIP_TRAILING_PROMPT_REGEX =
/(\r|\n)+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, terminate_command = nil) ⇒ MultiCommandParser

Returns a new instance of MultiCommandParser.



6
7
8
9
10
# File 'lib/repl_runner/multi_command_parser.rb', line 6

def initialize(commands, terminate_command = nil)
  @commands          = commands
  @terminate_command = terminate_command
  @raw               = ""
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



4
5
6
# File 'lib/repl_runner/multi_command_parser.rb', line 4

def commands
  @commands
end

#rawObject

Returns the value of attribute raw.



4
5
6
# File 'lib/repl_runner/multi_command_parser.rb', line 4

def raw
  @raw
end

Instance Method Details

#command_to_regex(command) ⇒ Object



12
13
14
# File 'lib/repl_runner/multi_command_parser.rb', line 12

def command_to_regex(command)
  /#{Regexp.quote(command)}\r*\n+/
end

#parse(string) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/repl_runner/multi_command_parser.rb', line 16

def parse(string)
  self.raw    = string.dup
  @parsed_result = []

  # remove terminate command
  string = string.gsub(command_to_regex(@terminate_command), '') if @terminate_command
  # attack the string from the end
  commands.reverse.each do |command|
    regex = command_to_regex(command)
    before, match, result = string.rpartition(regex)

    raise NoResults.new(command, raw) if result.empty?

    string = before
    @parsed_result << result.rpartition(STRIP_TRAILING_PROMPT_REGEX).first
  end

  @parsed_result.reverse!
  return @parsed_result
end