Class: Balotelli::Base

Inherits:
Object
  • Object
show all
Extended by:
Core::IRC, Core::Router, Core::Utils::ClassVariableGetter
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

Methods included from Core::Utils::ClassVariableGetter

class_variable_get

Constructor Details

#initialize(block, message, matchdata) ⇒ Base

Returns a new instance of Base.



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

def initialize(block, message, matchdata)
  @message = message
  @block = block
  @matchdata = matchdata
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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



74
75
76
77
78
# File 'lib/balotelli/base.rb', line 74

def inherited(subclass)
  super

  subclass.setup
end

.mod_match(str, priv = false) ⇒ Object



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

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



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

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



91
92
93
# File 'lib/balotelli/base.rb', line 91

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

.runObject



113
114
115
116
117
# File 'lib/balotelli/base.rb', line 113

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

.setupObject



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

def setup
  setup_router

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

Instance Method Details

#execute!Object Also known as: execute



21
22
23
# File 'lib/balotelli/base.rb', line 21

def execute!
  instance_exec(@message, @matchdata, &@block)
end

#get_user_list(channel) ⇒ Object



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

def get_user_list(channel)
  execute_in_mutex(channel) do
    self.class.names(channel)
  end
  cache[:user_list][channel]
end

#kick(params) ⇒ Object



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

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

#out(message_string, destination = nil) ⇒ Object



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

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