Class: AppleBot::CommandProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/applebot/command_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ CommandProxy

Returns a new instance of CommandProxy.



15
16
17
18
# File 'lib/applebot/command_proxy.rb', line 15

def initialize(namespace)
  @namespace = namespace
  @commands = []
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



6
7
8
# File 'lib/applebot/command_proxy.rb', line 6

def commands
  @commands
end

#namespaceObject

Returns the value of attribute namespace.



6
7
8
# File 'lib/applebot/command_proxy.rb', line 6

def namespace
  @namespace
end

Class Method Details

.for(command) ⇒ Object



8
9
10
11
12
13
# File 'lib/applebot/command_proxy.rb', line 8

def self.for(command)
  proxies[command.namespace] ||= new(command.namespace)
  proxy = proxies[command.namespace]
  proxy.add_command(command)
  proxy
end

Instance Method Details

#add_command(command) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/applebot/command_proxy.rb', line 20

def add_command(command)
  return if @commands.include?(command.action)
  @commands << command.action
  define_singleton_method(command.action, ->(options = {}) {
    AppleBot.run_command(command.file_name, options)
  })
end

#attach_to(klass) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/applebot/command_proxy.rb', line 28

def attach_to(klass)
  return if klass.respond_to?(self.namespace)
  namespace = self.namespace
  klass.define_singleton_method(namespace) {
    return AppleBot::CommandProxy.proxies[namespace]
  }
end

#inspectObject



36
37
38
39
40
41
# File 'lib/applebot/command_proxy.rb', line 36

def inspect
  s = self.to_s.gsub(">", " ")
  s << "commands: #{@commands.join(', ')}"
  s << ">"
  s
end