Module: Interop

Defined in:
lib/modules/interop.rb

Constant Summary collapse

DEBUG =
Debug.debug(File.basename(__FILE__))

Class Method Summary collapse

Class Method Details

.cleanly_load(targets, opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/modules/interop.rb', line 23

def self.cleanly_load(targets, opts)
  DEBUG.call "Package definitions #{targets}"
  result = wrap_constants(targets)
  if !opts[:save_the_environment]
    result.each_pair do |key, value|
      Object.send(:remove_const, key.to_sym) unless key.include?('::')
    end
  end

  result
end

.import(id, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/modules/interop.rb', line 11

def self.import(id, opts={})
  if @cache.include?(id)
    DEBUG.call "Cache hit #{id}"
    return @cache[id]
  end

  DEBUG.call "Load #{id}"
  snapshot = Module.constants
  require id
  cleanly_load(Module.constants - snapshot, opts)
end

.wrap_constants(new, result = {}, visited = Set.new) ⇒ Object



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
62
63
64
65
66
# File 'lib/modules/interop.rb', line 35

def self.wrap_constants(new, result={}, visited=Set.new)
  if new.length == 0
    return result
  end

  str = new.pop().to_s
  const = eval str
  if const.nil?
    return wrap_constants(new, result)
  end

  if const.respond_to? :constants
    children = const
      .constants
      .reject {|element| visited.include?(element)}
      .map {|child|
        visited.add(child)
        "#{str}::#{child.to_s}"
      }

    new += children
  end

  if const.class == Module
    const.private_instance_methods.each {|method| const.send(:public, method)}
    result[str] = Class.new.extend(const)
  else
    result[str] = const
  end

  return wrap_constants(new, result, visited)
end