Module: Kernel

Defined in:
lib/backload.rb,
lib/backload/load.rb,
lib/backload/require.rb,
lib/backload/require_relative.rb

Overview

require ‘backload/require_relative’

Class Method Summary collapse

Class Method Details

.backloaded(path, options = {}) ⇒ Object

Common callback for all loading methods: #load, #require and #require_relative.

Parameters:

  • options (Hash) (defaults to: {})

    The load callback options.

Options Hash (options):

  • :load (Boolean)

    True if #load was used.

  • :require (Boolean)

    True is #require or #require_relative was used.

  • :relative (Boolean)

    True is #require_relative was used.

  • :wrap (Boolean)

    The wrap argument passed to #load.



25
26
# File 'lib/backload.rb', line 25

def self.backloaded(path, options={})
end

.load(path, wrap = nil) ⇒ Object

Redefine Kernel.load with callback.



40
41
42
43
44
45
46
47
48
49
# File 'lib/backload/load.rb', line 40

def load(path, wrap=nil)
  result = load_without_callback(path, wrap)

  if result
    Kernel.loaded(path, wrap)
    Kernel.backloaded(path, :load=>true, :require=>false, :wrap=>wrap)
  end

  result
end

.load_without_callbackObject

Alias original Kernel.load method.



35
# File 'lib/backload/load.rb', line 35

alias_method :load_without_callback, :load

.loaded(path, wrap = nil) ⇒ Object

We create a noop method b/c it simplifes implementation. Just override this method to use.



7
8
# File 'lib/backload/load.rb', line 7

def self.loaded(path, wrap=nil)
end

.require(feature) ⇒ Object

Redefine Kernel.require with callback.



40
41
42
43
44
45
46
47
48
49
# File 'lib/backload/require.rb', line 40

def require(feature)
  result = require_without_callback(feature)

  if result
    Kernel.required(feature)
    Kernel.backloaded(feature, :require=>true, :load=>false)
  end

  result
end

.require_relative(feature) ⇒ Object

TODO:

It would nice if the ‘:relative` option were set to the realtive path, but that would require `Binding.of_caller`.

Redefine Kernel.require_relative with callback.



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

def require_relative(feature)
  #result = require_relative_without_callback(feature)

  ## We have to reimplement #require_relative instead of calling
  ## the alias to ensure the proper path is used. (*sad*)
  result = (
    c = caller.first
    fail "Can't parse #{c}" unless c.rindex(/:\d+(:in `.*')?$/)
    file = $` # File.dirname(c)
    if /\A\((.*)\)/ =~ file # eval, etc.
      raise LoadError, "require_relative is called in #{$1}"
    end
    #options[:relative] = File.dirname(file)
    absolute = File.expand_path(feature, File.dirname(file))
    require_without_callback absolute
  )

  if result
    Kernel.required_relative(feature)
    Kernel.backloaded(feature, :require=>true, :load=>false, :relative=>File.dirname(file))
  end

  result
end

.require_relative_without_callbackObject

Alias original Kernel.require_relative method.



55
# File 'lib/backload/require_relative.rb', line 55

alias_method :require_relative_without_callback, :require_relative

.require_without_callbackObject

Alias original Kernel.require method.



35
# File 'lib/backload/require.rb', line 35

alias_method :require_without_callback, :require

.required(path) ⇒ Object

We create a noop method b/c it simplifes implementation. Just override this method to use.



7
8
# File 'lib/backload/require.rb', line 7

def self.required(path)
end

.required_relative(path) ⇒ Object

We create a noop method b/c it simplifes implementation. Just override this method to use.

Presently this is an optional feature and must be required separately via ‘backload/require_relative`.



10
11
# File 'lib/backload/require_relative.rb', line 10

def self.required_relative(path)
end