| Class | Liftoff::ModelGenerator |
| In: |
vendor/plugins/liftoff/lib/model_generator.rb
|
| Parent: | Object |
Generate a model class for each side of any association declared in the CAL file.
# File vendor/plugins/liftoff/lib/model_generator.rb, line 8
8: def initialize
9: @log = Logger.new(STDOUT);
10: @log.level = Logger::ERROR;
11: end
# File vendor/plugins/liftoff/lib/model_generator.rb, line 13
13: def generate_model_files(associations, directory)
14: by_from = associations[0]
15: by_to = associations[1]
16:
17: # Extract the set of distinct models as implied by the associations array
18: # of hash tables. (See the comments for the Parser class.)
19: by_to_not_from = by_to.reject {|key, value| by_from[key]}
20: distinct_models = by_from.merge(by_to_not_from)
21:
22: # For each distinct model, create a model file in app/models and declare
23: # an association for every association specified for the corresponding
24: # entity in the CAL file.
25: distinct_models.each_key do |key|
26: path = computePathForModelName(key, directory)
27: @log.debug "\nWriting model '" + key + "' to " + File.expand_path(path)
28: File.open(path, "w") do |file|
29: assoc = nil;
30: classname = key.camelize;
31: @log.debug("Generating class: " + classname + ".");
32: file.puts("class " + classname + " < " + "ActiveRecord::Base")
33: if (values = by_from[key])
34: values.each do |value|
35: assoc = " " + value.from_mapping;
36: @log.debug("Adding FROM association: " + assoc + ".");
37: file.puts(assoc)
38: end
39: end
40: if values = by_to[key]
41: values.each do |value|
42: assoc = " " + value.to_mapping;
43: @log.debug("Adding TO association: " + assoc + ".");
44: file.puts(assoc)
45: end
46: end
47: file.puts("end")
48: end
49: end
50: end