Class: ReplRunner::MultiCommandParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, terminate_command = nil) ⇒ MultiCommandParser

Returns a new instance of MultiCommandParser.



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

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

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#rawObject

Returns the value of attribute raw.



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

def raw
  @raw
end

Instance Method Details

#command_to_regex(command) ⇒ Object



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

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

#parse(string) ⇒ Object



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

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)
    result_array = string.split(regex)
    @parsed_result << result_array.pop
    raise NoResults.new(command, raw) if @parsed_result.last.blank?
    string = result_array.join('')
  end

  @parsed_result.reverse!
  return @parsed_result
end