Class: ReplRunner::MultiCommandParser
- Inherits:
-
Object
- Object
- ReplRunner::MultiCommandParser
- Defined in:
- lib/repl_runner/multi_command_parser.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#raw ⇒ Object
Returns the value of attribute raw.
Instance Method Summary collapse
- #command_to_regex(command) ⇒ Object
-
#initialize(commands, terminate_command = nil) ⇒ MultiCommandParser
constructor
A new instance of MultiCommandParser.
- #parse(string) ⇒ Object
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
#commands ⇒ Object
Returns the value of attribute commands.
3 4 5 |
# File 'lib/repl_runner/multi_command_parser.rb', line 3 def commands @commands end |
#raw ⇒ Object
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 |