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

Returns a new instance of 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



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

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

Class Method Details

.inherited(subclass) ⇒ Object



72
73
74
75
76
# File 'lib/balotelli/base.rb', line 72

def inherited(subclass)
  super

  subclass.setup
end

.mod_match(str, priv = false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/balotelli/base.rb', line 93

def mod_match(str, priv = false)
  if str =~ /\A[^a-zA-Z0-9\s]?(\S+) ?(.*)/
    if (mod = @cache[:modules][Regexp.last_match(1).downcase])
      mod.match(Regexp.last_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



78
79
80
81
82
83
84
85
86
87
# File 'lib/balotelli/base.rb', line 78

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



89
90
91
# File 'lib/balotelli/base.rb', line 89

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

.runObject



109
110
111
112
113
# File 'lib/balotelli/base.rb', line 109

def run
  while (str = sgets)
    process(str)
  end
end

.setupObject



64
65
66
67
68
69
70
# File 'lib/balotelli/base.rb', line 64

def setup
  setup_router

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

Instance Method Details

#execute_in_mutex(channel) ⇒ Object



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

def execute_in_mutex(channel)
  cv = ConditionVariable.new
  if __CVS__.key? channel
    __CVS__[channel] << cv
  else
    __CVS__[channel] = [cv]
  end
  yield
  __MUTEX__.synchronize { cv.wait(__MUTEX__) }
end

#get_user_list(channel) ⇒ Object



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

def get_user_list(channel)
  execute_in_mutex(channel) do
    self.class.names(channel)
  end
  __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