module Facy::Command

Public Instance Methods

alias_commands() click to toggle source
# File lib/facy/command.rb, line 7
def alias_commands
  @alias_commands ||= {}
end
aliasing(origin, with) click to toggle source
# File lib/facy/command.rb, line 3
def aliasing(origin, with)
  alias_commands[with] = origin
end
command(pattern, options={}, &block) click to toggle source
# File lib/facy/command.rb, line 15
def command(pattern, options={}, &block)
  commands << {pattern: pattern, block: block}
end
commands() click to toggle source
# File lib/facy/command.rb, line 11
def commands
  @commands ||= []
end
execute(text) click to toggle source
# 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) click to toggle source
# File lib/facy/command.rb, line 43
def match_single_command(text)
  text =~ /^:(\S*)$/
  return $1
end
match_target_command(text) click to toggle source
# File lib/facy/command.rb, line 37
def match_target_command(text)
  text =~ /^:(\S*) (.+)$/
  return [$1, $2] if $1 && $2
  return nil
end