Class: McBlocky::Listener
- Inherits:
-
Object
- Object
- McBlocky::Listener
- Includes:
- Logging
- Defined in:
- lib/mcblocky/listener.rb
Constant Summary
Constants included from Logging
McBlocky::Logging::BLUE, McBlocky::Logging::BOLD, McBlocky::Logging::CYAN, McBlocky::Logging::GREEN, McBlocky::Logging::MAGENTA, McBlocky::Logging::RED, McBlocky::Logging::RESET, McBlocky::Logging::WHITE, McBlocky::Logging::YELLOW
Class Method Summary collapse
Instance Method Summary collapse
- #handle(modified, added, removed) ⇒ Object
-
#initialize(dir, main, &block) ⇒ Listener
constructor
A new instance of Listener.
- #start ⇒ Object
Methods included from Logging
#log_command, #log_error, #log_message, #log_server, #log_status
Constructor Details
#initialize(dir, main, &block) ⇒ Listener
Returns a new instance of Listener.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mcblocky/listener.rb', line 13 def initialize(dir, main, &block) @dir = dir @main = main @initial_files = [ @main, File.('../dsl.rb', __FILE__), File.('../dsl/commands.rb', __FILE__), File.('../dsl/repeat_chain.rb', __FILE__), File.('../dsl/impulse_chain.rb', __FILE__), File.('../dsl/selector.rb', __FILE__), File.('../dsl/command_block.rb', __FILE__), File.('../dsl/block.rb', __FILE__), File.('../dsl/container.rb', __FILE__), File.('../context.rb', __FILE__), File.('../executor.rb', __FILE__), ] @files = @initial_files @listener = Listen.to(dir, File.dirname(__FILE__), only: /\.rb$/, &method(:handle)) @handler = block end |
Class Method Details
Instance Method Details
#handle(modified, added, removed) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mcblocky/listener.rb', line 48 def handle(modified, added, removed) @files.each do |f| f = File.(f, @dir).gsub('\\','/') if modified.include? f or added.include? f or removed.include? f begin log_status "Reloading..." McBlocky.reload! result = Context.run_file(@main, @dir) rescue Exception => e log_error "Error in loaded file:" puts e.backtrace.join("\n\t") .sub("\n\t", ": #{e}#{e.class ? " (#{e.class})" : ""}\n\t") break end @handler.call(result) @files = @initial_files + result.required_files.to_a if result.required_files log_status "Reloaded." break end end end |
#start ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mcblocky/listener.rb', line 34 def start @listener.start begin log_status "Loading" result = Context.run_file(@main, @dir) rescue Exception log_error "Error in loaded file:" puts $! return end @handler.call(result) @files = @initial_files + result.required_files.to_a if result.required_files end |