Module: Requirium

Defined in:
lib/requirium.rb,
lib/version.rb,
lib/const_info.rb,
lib/load_loader.rb,
lib/require_loader.rb

Overview

Automatically calls Kernel#load or Kernel#require on first use. Example usage:

module M
  extend Requirium

  autoload :A
  autoload :B, 'b', 'b1', 'b2'
  autoload A: nil, B: ['b', 'b1', 'b2']
  autoload_relative :X
  autoload_relative :Y, 'y', 'y1', 'y2'
  autoload_relative X: nil, Y: ['y', 'y1', 'y2']

  autorequire :A
  autorequire :B, 'b', 'b1', 'b2'
  autorequire A: nil, B: ['b', 'b1', 'b2']
  autorequire_relative :X
  autorequire_relative :Y, 'y', 'y1', 'y2'
  autorequire_relative X: nil, Y: ['y', 'y1', 'y2']
end

Defined Under Namespace

Classes: ConstInfo, LoadLoader, RequireLoader

Constant Summary collapse

VERSION =
'0.0.6'.freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loader_threadObject (readonly)

Returns the value of attribute loader_thread.



51
52
53
# File 'lib/requirium.rb', line 51

def loader_thread
  @loader_thread
end

.queueObject (readonly)

Returns the value of attribute queue.



51
52
53
# File 'lib/requirium.rb', line 51

def queue
  @queue
end

Instance Method Details

#const_missing(sym) ⇒ Object

def const_defined?(*args)

Requirium.synchronize { super }

end



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/requirium.rb', line 66

def const_missing(sym)
  # if mri, use binding nesting
  nesting = nil
  if Requirium.mri?
    return unless nesting = caller_nesting
  end

  info = ConstInfo.new(self, sym, nesting)

  if Thread.current == Requirium.loader_thread
    # this avoids deadlocks. it uses the current loading to load the remaining dependencies
    has, value = internal_load(info)
    return has ? value : super
  end

  Requirium.queue.push(info)
  info.wait_ready
  raise info.error if info.error
  info.has_value? ? info.value : super
end