Module: Marshal

Defined in:
lib/utilrb/marshal/load_with_missing_constants.rb

Defined Under Namespace

Classes: BlackHole

Class Method Summary collapse

Class Method Details

.load_with_missing_constants(str_or_io) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/utilrb/marshal/load_with_missing_constants.rb', line 27

def self.load_with_missing_constants(str_or_io)
    if str_or_io.respond_to?(:tell)
        original_pos = str_or_io.tell
    end

    self.load(str_or_io)
rescue Exception => e
    case e.message
    when /undefined class\/module ((?:\w+::)+)$/
        names = $1.split('::')
        missing = names.pop
        base = names.inject(Object) { |m, n| m.const_get(n) }
        base.const_set(missing, Module.new)

        if original_pos
            str_or_io.seek(original_pos)
        end
        retry
    when /undefined class\/module ((?:\w+::)+)(\w+)$/
        mod, klass   = $1, $2
        full_name = "#{mod}#{klass}"
        mod = mod.split('::').inject(Object) { |m, n| m.const_get(n) }

        blackhole = Class.new(BlackHole) do
            @name = full_name
        end
        mod.const_set(klass, blackhole)

        if original_pos
            str_or_io.seek(original_pos)
        end
        retry
    end
    raise
end