Module: Lich::Common::HMR

Defined in:
lib/common/hmr.rb

Class Method Summary collapse

Class Method Details

.clear_cachevoid

This method returns an undefined value.

Clears the gem cache by calling ‘Gem.clear_paths`.

Examples:

Lich::Common::HMR.clear_cache


11
12
13
# File 'lib/common/hmr.rb', line 11

def self.clear_cache
  Gem.clear_paths
end

.loadedArray<String>

Retrieves a list of loaded Ruby files.

Examples:

loaded_files = Lich::Common::HMR.loaded

Returns:

  • (Array<String>)

    An array of paths to loaded Ruby files.



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

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

.msg(message) ⇒ void

This method returns an undefined value.

Sends a message to the appropriate output method.

If ‘_respond` is defined and the message contains “<b>”, it will call `_respond`. If `respond` is defined, it will call `respond`. Otherwise, it will print the message.

Examples:

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

Parameters:

  • message (String)

    The message to be sent or printed.



25
26
27
28
29
# File 'lib/common/hmr.rb', line 25

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

This method returns an undefined value.

Reloads files that match the given pattern.

This method clears the cache, finds files matching the pattern, and attempts to load them. If loading fails, it captures and reports the exception.

Examples:

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

Parameters:

  • pattern (Regexp)

    The pattern to match file paths against.

Raises:

  • (LoadError)

    If the file cannot be loaded.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/common/hmr.rb', line 53

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