Class: CommandHandler

Inherits:
BaseCommandHandler show all
Includes:
EventEmitter, Utils
Defined in:
lib/advanced_ruby_command_handler/command_handler.rb

Instance Attribute Summary collapse

Attributes inherited from BaseCommandHandler

#commands_dir, #commands_module_name, #events_dir, #events_module_name

Instance Method Summary collapse

Methods included from Utils

#find_command

Methods inherited from BaseCommandHandler

#get_module_by_string

Constructor Details

#initialize(options) ⇒ CommandHandler

Returns a new instance of CommandHandler.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 12

def initialize(options)
  super(options)
  @client = AdvancedRubyCommandHandler::Client.new
  @loaded_events = []

  Thread.new do
    Logger.info("Type '.reload' to load/reload all the commands & events files")
    loop do
      next unless $stdin.gets.chomp == ".reload"

      load_commands
      load_events
      Logger.check("All commands & events loaded")
    end
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 10

def client
  @client
end

#commandsObject (readonly)

Returns the value of attribute commands.



10
11
12
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 10

def commands
  @commands
end

#commands_moduleObject (readonly)

Returns the value of attribute commands_module.



10
11
12
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 10

def commands_module
  @commands_module
end

#eventsObject (readonly)

Returns the value of attribute events.



10
11
12
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 10

def events
  @events
end

#events_moduleObject (readonly)

Returns the value of attribute events_module.



10
11
12
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 10

def events_module
  @events_module
end

#loaded_eventsObject (readonly)

Returns the value of attribute loaded_events.



10
11
12
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 10

def loaded_events
  @loaded_events
end

Instance Method Details

#load_commandsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 29

def load_commands
  @commands = []
  Dir.entries(@commands_dir).each do |dir|
    next if %w[. ..].include?(dir)

    Dir.entries("#{@commands_dir}#{dir}").each do |file|
      next if %w[. ..].include?(file)

      load File.join(@commands_dir, dir, file)
      @commands << File.basename(file, ".rb")
    end
  end
  @commands_module = get_module_by_string(@commands_module_name)

  emit :commands_loaded, @commands
  self
end

#load_eventsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/advanced_ruby_command_handler/command_handler.rb', line 47

def load_events
  @events = []
  Dir.entries(@events_dir).each do |file|
    next if %w[. ..].include?(file)

    load File.join(@events_dir, file)
    @events << File.basename(file, ".rb")
  end

  Dir["#{File.dirname(__FILE__)}/defaults/events/*.rb"].sort.each do |default_event|
    next if @loaded_events.include?(File.basename(default_event, ".rb")) || @events.include?(File.basename(default_event, ".rb"))

    require default_event

    @events << File.basename(default_event, ".rb")
  end
  @event_module = get_module_by_string(@events_module_name)

  emit :events_loaded, @events

  @events.each do |event|
    next if @loaded_events.include?(event)

    @event_module.method(event).call(self)
    @loaded_events << event
  end

  self
end