Class: Build2Spec::Builder

Inherits:
Object show all
Defined in:
lib/build2spec.rb

Defined Under Namespace

Classes: UnresolvedPath

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.module_homesObject

Returns the value of attribute module_homes.



63
64
65
# File 'lib/build2spec.rb', line 63

def module_homes
  @module_homes
end

.standinsObject

Returns the value of attribute standins.



64
65
66
# File 'lib/build2spec.rb', line 64

def standins
  @standins
end

Class Method Details

.add_standin(nesting_path, name, standin) ⇒ Object



138
139
140
141
142
# File 'lib/build2spec.rb', line 138

def add_standin(nesting_path, name, standin)
  nesting_path = nesting_path.name.to_s if Module === nesting_path
  list = standin_location(nesting_path)
  list[name] = standin
end

.all_modulesObject



87
88
89
90
91
# File 'lib/build2spec.rb', line 87

def all_modules
  list = []
  ObjectSpace.each_object(Module){|m| list << m} 
  list
end

.buildObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/build2spec.rb', line 111

def build
  create_in = @create_files_in || $:.first

  FakingMethods::unknown_types.each do |type|
    type.guess!
  end

  @module_homes.each_pair do |mod, path|
    if path.respond_to? :resolve
      @module_homes[mod] = path.resolve(create_in)
    end
  end

  if @module_homes.values.empty?
    raise "No files designated to generate code into.  Use require or autoload to designate code destinations!"
  end

  @standins.each_value do |standin|
    unless File::exists?(@module_homes[standin.name])
      puts "Creating new code file: #{@module_homes[standin.name]}"
    end
    File::open(@module_homes[standin.name], "a") do |file|
      file.write(standin.mod_text + "\n")
    end
  end
end

.cover_for_module(klass) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/build2spec.rb', line 169

def cover_for_module(klass)
  m = /(?:(.*)::)?(.*)/.match klass.name.to_s
  nesting = m[1] || ""
  class_name = m[2]
  klass.class_eval { include CoveredModule }
  class_name = klass.name.to_s.sub(/.*::/, "")
  Build2Spec::Builder::add_standin(nesting, class_name, StandIn.new(klass))
end

.cover_for_modules(path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/build2spec.rb', line 70

def cover_for_modules(path)
  existing_modules = all_modules
  begin
    b2s_original_require(path)
  rescue LoadError
    return
  end

  new_modules = all_modules - existing_modules
  new_modules,contexts = new_modules.partition{|m| m.include?(Kernel)}
  new_modules.sort!{|l, r| l.name.delete("^:").length <=> r.name.delete("^:").length}

  new_modules.each do |mod|
    cover_for_module(mod)
  end
end

.create_covered_module(name, nesting, superclass = ::Object) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/build2spec.rb', line 178

def create_covered_module(name, nesting, superclass=::Object)
  classes = name.to_s.split("::")
  class_name = classes.pop
  classes.each do |nest|
    unless nesting.const_defined?(nest)
      create_covered_module(nest, nesting)
    end
    nesting = nesting.const_get(nest)
  end
  klass = Class.new(superclass)
  nesting.const_set(class_name, klass)
  UnknownType::add_guess_entry(klass, "#{nesting}::#{name}.new")
  cover_for_module(klass)
  return klass
end

.create_files_in(path) ⇒ Object



66
67
68
# File 'lib/build2spec.rb', line 66

def create_files_in(path)
  @create_files_in = path
end

.register_file(mod, file) ⇒ Object



93
94
95
# File 'lib/build2spec.rb', line 93

def register_file(mod, file)
  module_homes[mod] = resolve_path(file)
end

.resolve_path(file) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/build2spec.rb', line 97

def resolve_path(file)
  $:.each do |lib_path|
    path = File::expand_path(File::join(lib_path, file))
    unless /.rb$/ =~ path
      path += ".rb"
    end
    if File::exists?(path) and File::file?(path)
      return path
    end
  end

  return UnresolvedPath.new(file)
end

.standin_for(nesting_path) ⇒ Object



144
145
146
147
148
149
# File 'lib/build2spec.rb', line 144

def standin_for(nesting_path)
  nesting_path = nesting_path.name.to_s if Module === nesting_path
  m = /(?:(.*)::)?(.*)/.match nesting_path
  list = standin_location(m[1])
  list[m[2]]
end

.standin_location(nesting_path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/build2spec.rb', line 151

def standin_location(nesting_path)
  list = @standins
  return list if nesting_path.nil?
  classpath = nesting_path.split("::")
  create_missing_under = ::Object
  classpath.each do |klass|
    unless list.has_key?(klass)
      unless create_missing_under.const_defined?(klass.intern)
        raise RuntimeError, "Missing #{klass} in #{classpath}"
      end
      list[klass] = StandIn.new(create_missing_under.const_get(klass.intern))
    end
    create_missing_under = list[klass].covering_for
    list = list[klass].nested
  end
  return list
end