Module: VirtFS::Activation

Included in:
VirtFS
Defined in:
lib/virtfs/activation.rb

Instance Method Summary collapse

Instance Method Details

#activate!(enable_require = false) ⇒ Object

Overrides Ruby’s native Dir, File, and IO classes with corresponding VirtFS classes



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/virtfs/activation.rb', line 17

def activate!(enable_require = false)
  activate_mutex.synchronize do
    raise "VirtFS.activate! already activated" if @activated
    @activated = true

    Object.class_eval do
      remove_const(:Dir)
      remove_const(:File)
      remove_const(:IO)
      remove_const(:Pathname)

      const_set(:Dir,      VirtFS::VDir)
      const_set(:File,     VirtFS::VFile)
      const_set(:IO,       VirtFS::VIO)
      const_set(:Pathname, VirtFS::VPathname)
    end

    if enable_require
      VirtFS::Kernel.inject
      VirtFS::Kernel.enable
    end
  end
  true
end

#activate_mutexObject



3
4
5
# File 'lib/virtfs/activation.rb', line 3

def activate_mutex
  @activate_mutex ||= Mutex.new
end

#activated?Boolean

Returns indicating if VirtFS is active.

Returns:

  • (Boolean)

    indicating if VirtFS is active

See Also:



11
12
13
# File 'lib/virtfs/activation.rb', line 11

def activated?
  @activated
end

#deactivate!Object

Restores Ruby’s native Dir, File, and IO classes to their defaults



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/virtfs/activation.rb', line 44

def deactivate!
  activate_mutex.synchronize do
    raise "VirtFS.deactivate! not activated" unless @activated
    @activated = false

    Object.class_eval do
      remove_const(:Dir)
      remove_const(:File)
      remove_const(:IO)
      remove_const(:Pathname)

      const_set(:Dir,      VfsRealDir)
      const_set(:File,     VfsRealFile)
      const_set(:IO,       VfsRealIO)
      const_set(:Pathname, VfsRealPathname)
    end
    VirtFS::Kernel.disable
  end
  true
end

#with(enable_require = false) ⇒ Object

Invokes the given block in an activated context

See Also:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/virtfs/activation.rb', line 68

def with(enable_require = false)
  if activated?
    yield
  else
    begin
      activate!(enable_require)
      yield
    ensure
      deactivate!
    end
  end
end

#withoutObject

Invokes the given block in a deactivated context

See Also:



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/virtfs/activation.rb', line 84

def without
  if !activated?
    yield
  else
    begin
      deactivate!
      yield
    ensure
      activate!
    end
  end
end