Class: Balotelli::Base

Inherits:
Object
  • Object
show all
Extended by:
Core::IRC, Core::Router
Defined in:
lib/balotelli/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Router

match, match?, on, setup_router

Methods included from Core::IRC

configure, connect, join, names, pong, privmsg, sgets, sputs

Constructor Details

#initialize(str, route, block, message, command) ⇒ Base



12
13
14
15
16
17
18
19
# File 'lib/balotelli/base.rb', line 12

def initialize(str, route, block, message, command)
  @str = str
  @message = message
  @block = block
  @route = route
  @command = command
  @matchdata = @command.match(@route)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **params, &block) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/balotelli/base.rb', line 42

def method_missing(method, *args, **params, &block)
  if method =~ /__([[[:upper:]]_]+)__/
    self.class.instance_variable_get("@#{$1}".downcase)
  else
    super
  end
end

Class Method Details

.inherited(subclass) ⇒ Object



62
63
64
65
66
# File 'lib/balotelli/base.rb', line 62

def inherited(subclass)
  super

  subclass.setup
end

.mod_match(str, priv = false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/balotelli/base.rb', line 83

def mod_match(str, priv = false)
  if str =~ /\A[^a-zA-Z0-9\s]?(\S+) ?(.*)/
    if (mod = @cache[:modules][$1.downcase])
      mod.match($2, priv)
    end
  elsif str == :join
    match = nil
    @modules.each do |_, m|
      if (match = m.match(:join))
        break(match)
      end
    end
    match
  end
end

.register(mod, namespace = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/balotelli/base.rb', line 68

def register(mod, namespace = nil)
  if namespace
    mod.instance_variable_set(:@commands_namespace, namespace.intern)
    mod.instance_exec(namespace) do |n|
      define_method(:commands_namespace) { n.intern }
    end
  end
  @modules[mod.commands_namespace] ||= mod
  @cache[:modules] = Hash[@modules.map { |k, v| [k.to_s, v] }]
end

.register_special(mod) ⇒ Object



79
80
81
# File 'lib/balotelli/base.rb', line 79

def register_special(mod)
  @routes.merge!(mod.instance_variable_get(:@routes))
end

.runObject



99
100
101
102
103
104
# File 'lib/balotelli/base.rb', line 99

def run
  @to_process = false
  while (str = sgets)
    process(str)
  end
end

.setupObject



54
55
56
57
58
59
60
# File 'lib/balotelli/base.rb', line 54

def setup
  setup_router

  @modules = {}
  @cache = Hash.new { |hash, key| hash[key] = {} }
  @mutex = Mutex.new
end

Instance Method Details

#get_user_list(channel) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/balotelli/base.rb', line 34

def get_user_list(channel)
  self.class.instance_variable_set(:@to_process, :user_list)
  cv = self.class.instance_variable_set(:@condition_variable, ConditionVariable.new)
  self.class.names(channel)
  __MUTEX__.synchronize { cv.wait(__MUTEX__) }
  __CACHE__[:user_list][channel]
end

#kick(params) ⇒ Object



30
31
32
# File 'lib/balotelli/base.rb', line 30

def kick(params)
  Core::Kick.new(params, self.class).execute!
end

#out(message_string, destination = nil) ⇒ Object



26
27
28
# File 'lib/balotelli/base.rb', line 26

def out(message_string, destination = nil)
  self.class.privmsg((destination || @message.responder), message_string)
end