Earthquake::Input

Public Instance Methods

alias_command(name, target) click to toggle source
# File lib/earthquake/input.rb, line 33
def alias_command(name, target)
  name = name.is_a?(Symbol) ? ":#{name}" : name.to_s
  target = target.is_a?(Symbol) ? ":#{target}" : target.to_s
  command_aliases[name] = target
end
ask(message) click to toggle source
# File lib/earthquake/input.rb, line 88
def ask(message)
  print message
  (STDIN.gets || "").chomp
end
async_e(&block) click to toggle source
# File lib/earthquake/input.rb, line 93
def async_e(&block)
  async { handle_api_error(&block) }
end
command(pattern, options = {}, &block) click to toggle source
# File lib/earthquake/input.rb, line 53
def command(pattern, options = {}, &block)
  if block
    if pattern.is_a?(String) || pattern.is_a?(Symbol)
      command_name = ":#{pattern}"
      command_names << command_name
      if block.arity > 0
        pattern = %^#{command_name}\s+(.*)$|
      else
        pattern = %^#{command_name}$|
      end
    end
    command_names << ":#{options[:as]}" if options[:as]
    commands << {:pattern => pattern, :block => block}
  else
    commands.detect {|c| c[:pattern] =~ pattern}
  end
end
command_aliases() click to toggle source
# File lib/earthquake/input.rb, line 29
def command_aliases
  @command_aliases ||= {}
end
command_names() click to toggle source
# File lib/earthquake/input.rb, line 17
def command_names
  @command_names ||= Set.new
end
commands() click to toggle source
# File lib/earthquake/input.rb, line 13
def commands
  @commands ||= []
end
completion(&block) click to toggle source
# File lib/earthquake/input.rb, line 25
def completion(&block)
  completions << block
end
completions() click to toggle source
# File lib/earthquake/input.rb, line 21
def completions
  @completions ||= []
end
confirm(message, type = config[:confirm_type]) click to toggle source
# File lib/earthquake/input.rb, line 71
def confirm(message, type = config[:confirm_type])
  s = case type
      when :y
        ask("#{message} [Yn] ".u)
      when :n
        ask("#{message} [yN] ".u)
      else
        raise "type must be :y or :n"
      end
  s = type.to_s if s.empty?
  if m = s.match(/^[yn]$/)
    return m[0].downcase == 'y'
  else
    confirm(message, type)
  end
end
handle_api_error(&block) click to toggle source
# File lib/earthquake/input.rb, line 97
def handle_api_error(&block)
  result = block.call
  if result["error"]
    notify "[ERROR] #{result["error"]}"
  end
end
input(text) click to toggle source
# File lib/earthquake/input.rb, line 39
def input(text)
  return if text.empty?

  input_filters.each { |f| text = f.call(text) }

  if command = command(text)
    command[:block].call(command[:pattern].match(text))
  elsif !text.empty?
    puts "Command not found".c(43)
  end
rescue Exception => e
  error e
end
input_filter(&block) click to toggle source
# File lib/earthquake/input.rb, line 9
def input_filter(&block)
  input_filters << block
end
input_filters() click to toggle source
# File lib/earthquake/input.rb, line 5
def input_filters
  @input_filters ||= []
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.