Module: Facy::Command

Included in:
Facy
Defined in:
lib/facy/command.rb

Instance Method Summary collapse

Instance Method Details

#alias_commandsObject



7
8
9
# File 'lib/facy/command.rb', line 7

def alias_commands
  @alias_commands ||= {}
end

#aliasing(origin, with) ⇒ Object



3
4
5
# File 'lib/facy/command.rb', line 3

def aliasing(origin, with)
  alias_commands[with] = origin
end

#command(pattern, options = {}, &block) ⇒ Object



15
16
17
# File 'lib/facy/command.rb', line 15

def command(pattern, options={}, &block)
  commands << {pattern: pattern, block: block}
end

#commandsObject



11
12
13
# File 'lib/facy/command.rb', line 11

def commands
  @commands ||= []
end

#execute(text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/facy/command.rb', line 19

def execute(text)
  text.strip!
  rule, target = match_single_command(text) || match_target_command(text)
  origin = alias_commands[rule.to_sym] if rule
  rule = origin.nil? ? rule : origin

  commands.each do |c|
    if rule.to_s == c[:pattern].to_s.split(":").first
      c[:block].call(target)
      return
    end
  end
rescue Exception => e
  error e
  log(:error, e.message)
  log(:error, e.backtrace)
end

#match_single_command(text) ⇒ Object



43
44
45
46
# File 'lib/facy/command.rb', line 43

def match_single_command(text)
  text =~ /^:(\S*)$/
  return $1
end

#match_target_command(text) ⇒ Object



37
38
39
40
41
# File 'lib/facy/command.rb', line 37

def match_target_command(text)
  text =~ /^:(\S*) (.+)$/
  return [$1, $2] if $1 && $2
  return nil
end