Class: ConsoleRtm::InputParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rmilk/input_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(input) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rmilk/input_parser.rb', line 4

def parse(input)
  parts = input.split(' ').map {|e| preprocess e}
  res, last_command, first_command = {}, 'first', nil
  parts.each do |e|
    if command? e then
      res[e] = true
      last_command = e
      first_command ||= e
    else
      t = res[last_command]
      value_is_not_set = (t == true || t.nil?)
      res[last_command] = value_is_not_set ? e : "#{t} #{e}"
    end
  end
  res['command'] = first_command if !first_command.nil?
  res
end