Module: FakeFS::Require

Included in:
Kernel
Defined in:
lib/fakefs/require.rb,
lib/fakefs/require/version.rb

Defined Under Namespace

Modules: Autoload, Load

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activate!(opts = {}) ⇒ Object

Activates faked #require

Options

  • :fallback => true # activates the fallback to Kernel#require

  • :autoload => true # activates faked #autoload, #autoload? and #const_missing

  • :load => true # activates faked #load


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fakefs/require.rb', line 15

def self.activate! opts = {}
  return if active?
  
  @active = true
  
  @opts = {
    :fallback => false,
    :autoload => false,
    :load => false,
  }.merge opts
  
  Kernel.class_eval do
    alias_method :fakefs_original_require, :require
    alias_method :require, :fakefs_require
  end
  
  Module.class_eval do
    alias_method :fakefs_original_autoload, :autoload
    alias_method :autoload, :fakefs_autoload
    
    alias_method :fakefs_original_autoload?, :autoload?
    alias_method :autoload?, :fakefs_autoload?
    
    alias_method :fakefs_original_const_missing, :const_missing
    alias_method :const_missing, :fakefs_const_missing
  end if @opts[:autoload]
  
  Kernel.class_eval do
    alias_method :fakefs_original_load, :load
    alias_method :load, :fakefs_load
  end if @opts[:load]
end

.active?Boolean

Returns whether FakeFS::Require is active

Returns:

  • (Boolean)

72
73
74
# File 'lib/fakefs/require.rb', line 72

def self.active?
  @active
end

.autoloadableObject

Returns a hash containing autoload data


82
83
84
# File 'lib/fakefs/require.rb', line 82

def self.autoloadable
  @autoloadable ||= {}
end

.clearObject

Clears the autoload data hash


87
88
89
# File 'lib/fakefs/require.rb', line 87

def self.clear
  @autoloadable = {}
end

.deactivate!Object

Deactivates the faked methods


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fakefs/require.rb', line 49

def self.deactivate!
  return unless active?
  
  @active = false
  
  Kernel.class_eval do
    alias_method :require, :fakefs_original_require
  end
  
  Module.class_eval do
    alias_method :autoload, :fakefs_original_autoload
    alias_method :autoload?, :fakefs_original_autoload?
    alias_method :const_missing, :fakefs_original_const_missing
  end if @opts[:autoload]
  
  Kernel.class_eval do
    alias_method :load, :fakefs_original_load
  end if @opts[:load]
  
  @opts = nil
end

.optsObject

Returns the options passed to ::activate!


77
78
79
# File 'lib/fakefs/require.rb', line 77

def self.opts
  @opts
end

.resolve(fn) ⇒ Object

Resolves the passed filename to a path


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fakefs/require.rb', line 92

def self.resolve fn
  found = nil
  if fn[0...1] == "/"
    found = fn if File.exist? fn
  else
    $LOAD_PATH.each do |p|
      path = p + "/" + fn
      found = path if File.exist? path
    end
  end
  
  return found
end

Instance Method Details

#fakefs_require(fn) ⇒ Object

Faked #require (see Kernel#require)

Raises:

  • (LoadError)

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fakefs/require.rb', line 107

def fakefs_require fn
  fn = fn.to_s
  orig_fn = fn.dup
  
  fn = fn + ".rb" unless fn[-3..-1] == ".rb"
  path = FakeFS::Require.resolve fn
  
  if path
    return false if $".include? fn
    
    File.open(path, "r") {|f| Object.class_eval f.read, fn, 1 }
    $" << fn
    return true
  elsif FakeFS::Require.opts[:fallback]
    opts = FakeFS::Require.opts
    begin
      FakeFS.deactivate!
      FakeFS::Require.deactivate!
      return fakefs_original_require orig_fn
    ensure
      FakeFS::Require.activate! opts
      FakeFS.activate!
    end
  end
  
  raise LoadError, "no such file to load -- " + orig_fn
end