Module: Lich::Common::HMR

Defined in:
documented/common/hmr.rb

Overview

Hot Module Reloading functionality This module provides methods to clear cache, reload files, and send messages.

Examples:

Reloading a module

Lich::Common::HMR.reload(/my_module/)

Class Method Summary collapse

Class Method Details

.clear_cachevoid

This method returns an undefined value.

Clears the gem load paths cache

Examples:

Clearing the cache

Lich::Common::HMR.clear_cache


17
18
19
# File 'documented/common/hmr.rb', line 17

def self.clear_cache
  Gem.clear_paths
end

.loadedArray<String>

Returns a list of loaded Ruby files

Examples:

Getting loaded files

loaded_files = Lich::Common::HMR.loaded

Returns:

  • (Array<String>)

    An array of loaded Ruby file paths.



37
38
39
# File 'documented/common/hmr.rb', line 37

def self.loaded
  $LOADED_FEATURES.select { |path| path.end_with?(".rb") }
end

.msg(message) ⇒ void

Note:

If the message contains HTML tags, it will be handled differently.

This method returns an undefined value.

Sends a message to the appropriate output

Examples:

Sending a message

Lich::Common::HMR.msg("Hello, World!")

Parameters:

  • message (String)

    The message to be sent



27
28
29
30
31
# File 'documented/common/hmr.rb', line 27

def self.msg(message)
  return _respond message if defined?(:_respond) && message.include?("<b>")
  return respond message if defined?(:respond)
  puts message
end

.reload(pattern) ⇒ void

Note:

This method clears the cache before reloading.

This method returns an undefined value.

Reloads files matching the given pattern

Examples:

Reloading files

Lich::Common::HMR.reload(/my_pattern/)

Parameters:

  • pattern (Regexp)

    The regex pattern to match file paths

Raises:

  • (LoadError)

    If a file cannot be loaded



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'documented/common/hmr.rb', line 48

def self.reload(pattern)
  self.clear_cache
  loaded_paths = self.loaded.grep(pattern)
  unless loaded_paths.empty?
    loaded_paths.each { |file|
      begin
        load(file)
        self.msg "<b>[lich.hmr] reloaded %s</b>" % file
      rescue => exception
        self.msg exception
        self.msg exception.backtrace.join("\n")
      end
    }
  else
    self.msg "<b>[lich.hmr] nothing matching regex pattern: %s</b>" % pattern.source
  end
end