Module: Modules

Defined in:
lib/modules.rb

Class Method Summary collapse

Class Method Details

.external_import(name) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/modules.rb', line 32

def self.external_import(name)
  if @cache.include? name
    puts "Already cached external module #{name}"
    return @cache[name]
  end

  puts "Loading external module #{name}"
  snapshot = Module.constants
  require name
  defined = Module.constants - snapshot
  p defined
  plucked = []
  resolved = {}
  while defined.length > 0
    todo = defined.pop
    plucked.push todo
    str = todo.to_s
    const = eval str
    next if const.nil?
    resolved[str] = const.class == Module ? Class.new.extend(const) : const
    if const.respond_to? :constants
      children = const
        .constants
        .map {|child| "#{str}::#{child.to_s}"}
        .select {|id|
          parts = id.split '::'
          groups = parts.group_by {|x| x}
          groups.values.all? {|group| group.length < 3}
        }

      defined += children
    end
  end

  puts "Found #{resolved}"
  @cache[name] = resolved
  plucked
    .select {|item| item.class == Symbol}
    .each {|item| Object.send(:remove_const, item)}
  return resolved
end

.internal_import(dirname, basename) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/modules.rb', line 14

def self.internal_import(dirname, basename)
  path = File.join @basepath, dirname, basename
  if @cache.include? @path
    puts "Already cached local module #{path}"
    return @cache[path]
  end

  puts "Loading local module #{path}"
  prev_dirname = @dirname
  prev_path = @path
  @dirname = dirname
  @path = path
  require @path
  @dirname = prev_dirname
  @path = prev_path
  return @cache[path]
end

.main(cmd, args, opts) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/modules.rb', line 85

def self.main(cmd, args, opts)
  case cmd
  when 'run'
    run(args, opts)
  else
    raise "Invalid command #{cmd}"
  end
end

.run(args, opts) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/modules.rb', line 74

def self.run(args, opts)
  file = args[0]
  abs = "#{Dir.pwd}/#{file}"
  dirname = File.dirname abs
  basename = File.basename abs

  puts "Set default basepath to #{dirname}"
  @basepath = dirname
  internal_import '', basename
end