Class: Verbify::CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/verbify/command_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ CommandParser

Returns a new instance of CommandParser.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
# File 'lib/verbify/command_parser.rb', line 18

def initialize
  @actions = { nil => { :proc => proc {} } }
  @ignorable = []
  @separators = []
  @error_proc = proc { puts "Invalid" }
  yield self if block_given?
end

Instance Method Details

#else(&block) ⇒ Object



40
41
42
# File 'lib/verbify/command_parser.rb', line 40

def else(&block)
  @error_proc = block
end

#exec!(input) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/verbify/command_parser.rb', line 44

def exec!(input)
  words = input.downcase.split.delete_if { |word| @ignorable.include? word }
  commands = []

  loop do
    commands << words.take_while { |word| not @separators.include? word }
    sep = words.index { |word| @separators.include? word }
    break if sep.nil?
    words = words.drop(sep + 1)
  end

  commands.each { |args| exec_command! args }
end

#ignore(ignore) ⇒ Object



26
27
28
# File 'lib/verbify/command_parser.rb', line 26

def ignore(ignore)
  @ignorable += [*ignore]
end

#on(possible_verbs, modifiers = [], &block) ⇒ Object



34
35
36
37
38
# File 'lib/verbify/command_parser.rb', line 34

def on(possible_verbs, modifiers=[], &block)
  [*possible_verbs].each do |v|
    @actions[v] = { :proc => block, :modifiers => modifiers }
  end
end

#split_by(seps) ⇒ Object



30
31
32
# File 'lib/verbify/command_parser.rb', line 30

def split_by(seps)
  @separators += [*seps]
end