module Aws::Templates::Utils::Autoload

Lazy load implementation

It allows to skip 'require' definitions and load all classes and modules by convention.

Constants

MODULE_LOCKER
REQUIRE_LOCKER
Trace

Public Class Methods

_check_if_required(path, obj) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 28
def self._check_if_required(path, obj)
  raise(obj) if obj.is_a?(::Exception)
  return if obj.owned?

  obj.lock.unlock
  locker_obj = REQUIRE_LOCKER[path]
  raise locker_obj if locker_obj.is_a?(Exception)
end
_try_to_require(path, mutex) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 19
def self._try_to_require(path, mutex)
  require path
rescue ScriptError, NoMemoryError, StandardError => e
  REQUIRE_LOCKER.get_and_set(path, e)
  raise e
ensure
  mutex.unlock
end
atomic_require(path) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 37
def self.atomic_require(path)
  mutex = Mutex.new.lock

  obj = REQUIRE_LOCKER.put_if_absent(path, mutex)

  if obj.nil?
    _try_to_require(path, mutex)
  else
    _check_if_required(path, obj)
  end

  true
end
autoload!(mod) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 51
def self.autoload!(mod)
  return if mod.name.nil?

  MODULE_LOCKER.compute_if_absent(mod) do
    path = mod.pathize

    begin
      atomic_require path
    rescue LoadError => e
      sanitize_load_exception(e, path)
    end

    true
  end
end
const_is_loaded?(mod, const_name) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 81
def self.const_is_loaded?(mod, const_name)
  const_path = Autoload.const_path_for(mod, const_name)

  begin
    atomic_require const_path
    true
  rescue LoadError => e
    Autoload.sanitize_load_exception(e, const_path)
    false
  end
end
const_path_for(mod, const_name) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 67
def self.const_path_for(mod, const_name)
  raise ScriptError.new("Autoload is not supported for #{mod}") if mod.name.nil?

  path = const_name.to_s.pathize

  path = "#{mod.pathize}/#{path}" unless mod.root_namespace?

  path
end
sanitize_load_exception(e, path) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 77
def self.sanitize_load_exception(e, path)
  raise e unless e.path == path
end

Public Instance Methods

const_missing(const_name) click to toggle source
Calls superclass method
# File lib/aws/templates/utils/autoload.rb, line 93
def const_missing(const_name)
  super(const_name) unless Autoload.const_is_loaded?(self, const_name)

  unless const_defined?(const_name)
    raise NameError.new(
      "::#{self}::#{const_name} is loaded but the constant is missing"
    )
  end

  const_get(const_name)
end
lazy() click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 105
def lazy
  Lazy.new(self)
end
reduce(_ = false) click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 109
def reduce(_ = false)
  self
end
root_namespace?() click to toggle source
# File lib/aws/templates/utils/autoload.rb, line 113
def root_namespace?
  (self == ::Kernel) || (self == ::Object)
end