Class: Muzang::Plugins::Eval

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/muzang-plugins/muzang-eval.rb

Instance Method Summary collapse

Methods included from Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ Eval

Returns a new instance of Eval.



25
26
27
# File 'lib/muzang-plugins/muzang-eval.rb', line 25

def initialize(bot)
  @bot = bot
end

Instance Method Details

#call(connection, message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/muzang-plugins/muzang-eval.rb', line 29

def call(connection, message)
  on_channel(message) do
    match(message, /^\% (.*)/) do |match|
      operation = proc do
        safe(match[1])
      end
      callback = proc do |tuple|
        result, error = tuple
        connection.msg(message.channel, "#{result}") if result
        connection.msg(message.channel, "Error: #{error}") if error
      end
      EM.defer(operation, callback)
    end
  end
end

#safe(*args, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/muzang-plugins/muzang-eval.rb', line 45

def safe(*args, &block)
  unless args.first =~ /EM|EventMachine/
    @safe ||= Safe.new
    @safe.safe(*args, &block)
  end
end