Module: SceneSwitcher

Defined in:
lib/scene_switcher.rb,
lib/scene_switcher/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.cache(file, priv = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scene_switcher.rb', line 24

def cache(file, priv = false)
  begin
    expr = File.binread(file)
  rescue
    raise LoadError, "cannot load such file -- #{file}"
  end
  encoding = "UTF-8"
  encoding = $1 if expr.lines[0..2].join("\n").match(/coding:\s*(\S+)/)
  expr.force_encoding(encoding)
  if priv
    @proc_cache[file] = eval("Proc.new {\nmodule Module.new::SceneSwitcherTemporaryModule\n#{expr}\nend\n}", Object::TOPLEVEL_BINDING, file, -1)
  else
    @proc_cache[file] = eval("Proc.new {\n#{expr}\n}", Object::TOPLEVEL_BINDING, file, 0)
  end
end

.force_gcObject



18
19
20
21
22
# File 'lib/scene_switcher.rb', line 18

def force_gc
  tmp_gc_disable = GC.enable
  GC.start
  GC.disable if tmp_gc_disable
end

.gc_on_switchObject



10
11
12
# File 'lib/scene_switcher.rb', line 10

def gc_on_switch
  @gc_on_switch
end

.gc_on_switch=(bool) ⇒ Object



14
15
16
# File 'lib/scene_switcher.rb', line 14

def gc_on_switch=(bool)
  @gc_on_switch = bool
end

.switch_to(file, priv = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/scene_switcher.rb', line 40

def switch_to(file, priv = false)
  if @file
    @file = file
    throw :sceneswitcher_switch_signal
  else
    @file = file
    while @file
      force_gc if @gc_on_switch
      catch(:sceneswitcher_switch_signal) do
        cache(@file, priv) unless @proc_cache[@file]
        @proc_cache[@file].call
        @file = nil
      end
    end
    exit
  end
end