Class: Rmre::Generator
- Inherits:
-
Object
- Object
- Rmre::Generator
- Defined in:
- lib/rmre/generator.rb
Constant Summary collapse
- SETTINGS_ROOT =
File.('../../../../db', __FILE__)
- BEGIN_TAG =
"##########BEGIN-EZMODEL-AUTO-GENERATED-SECTION##########"
- END_TAG =
"##########END-EZMODEL-AUTO-GENERATED-SECTION##########"
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#output_path ⇒ Object
readonly
Returns the value of attribute output_path.
Instance Method Summary collapse
- #connect ⇒ Object
- #create_model(table_name) ⇒ Object
- #create_models(tables) ⇒ Object
- #dump_schema(stream) ⇒ Object
- #foreign_keys ⇒ Object
-
#initialize(options, out_path, include) ⇒ Generator
constructor
A new instance of Generator.
- #process?(table_name) ⇒ Boolean
Constructor Details
#initialize(options, out_path, include) ⇒ Generator
Returns a new instance of Generator.
16 17 18 19 20 21 |
# File 'lib/rmre/generator.rb', line 16 def initialize(, out_path, include) @connection_options = @connection = nil @output_path = out_path @include_prefixes = include end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/rmre/generator.rb', line 8 def connection @connection end |
#output_path ⇒ Object (readonly)
Returns the value of attribute output_path.
9 10 11 |
# File 'lib/rmre/generator.rb', line 9 def output_path @output_path end |
Instance Method Details
#connect ⇒ Object
23 24 25 26 27 28 |
# File 'lib/rmre/generator.rb', line 23 def connect return if @connection_options.empty? ActiveRecord::Base.establish_connection(@connection_options) @connection = ActiveRecord::Base.connection end |
#create_model(table_name) ⇒ Object
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rmre/generator.rb', line 44 def create_model(table_name) constraints = [] foreign_keys.each do |fk| src = constraint_src(table_name, fk) constraints << src unless src.nil? end _file_path = File.join(output_path, "#{table_name.tableize.singularize}.rb") if (!File.exists?(_file_path)) content = generate_model_source(table_name, constraints) else # read file content file = File.open(_file_path, "r+") content = "" line_begin = -1 line_end = -1 i = -1 while (!file.eof?) i = i+1 _line = file.readline # remove old code if (_line.include?(table_name.tableize.classify) && _line.include?("EZModel::ActiveRecord")) _line = "class #{table_name.tableize.classify} < ActiveRecord::Base \n" end if (_line.include?(BEGIN_TAG)) line_begin = i end if (_line.include?(END_TAG)) line_end = i end content += _line end file.close() # remove auto generated code if (line_begin<line_end) content = content.gsub(/#{BEGIN_TAG}(.*)#{END_TAG}/im, "") end _text = content content = "" _text.split("\n").each do |_line| # remove include header from old version unless (_line.include?("require File.expand_path('../ez_models")) content += _line + "\n" end # add auto-gen code below class if (_line.include?(table_name.tableize.classify) && _line.include?("ActiveRecord::Base")) _body = generate_model_content(table_name, constraints) content += _body + "\n" end end end File.open(_file_path, "w") do |file| file.write(content) end end |
#create_models(tables) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/rmre/generator.rb', line 30 def create_models(tables) return unless tables.is_a? Array FileUtils.mkdir_p(@output_path) if !Dir.exists?(@output_path) tables.each do |table_name| create_model(table_name) if process?(table_name) end end |
#dump_schema(stream) ⇒ Object
40 41 42 |
# File 'lib/rmre/generator.rb', line 40 def dump_schema(stream) ActiveRecord::SchemaDumper.dump_with_fk(connection, foreign_keys, stream) end |
#foreign_keys ⇒ Object
123 124 125 |
# File 'lib/rmre/generator.rb', line 123 def foreign_keys @foreign_keys ||= fetch_foreign_keys end |
#process?(table_name) ⇒ Boolean
113 114 115 116 117 118 119 120 121 |
# File 'lib/rmre/generator.rb', line 113 def process?(table_name) return true if @include_prefixes.nil? || @include_prefixes.empty? @include_prefixes.each do |prefix| return true if table_name =~ /^#{prefix}/ end false end |