Class: DBD4::RailsModelFile
- Inherits:
-
Object
- Object
- DBD4::RailsModelFile
- Defined in:
- lib/dbd4/rails_model_file.rb
Instance Attribute Summary collapse
-
#modelfile ⇒ Object
writeonly
Sets the attribute modelfile.
-
#modelname ⇒ Object
writeonly
Sets the attribute modelname.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #generate ⇒ Object
- #init_variables ⇒ Object
-
#initialize(options = {}) ⇒ RailsModelFile
constructor
A new instance of RailsModelFile.
- #load ⇒ Object
- #setContent(string) ⇒ Object
- #to_str ⇒ Object
- #update(table) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RailsModelFile
Returns a new instance of RailsModelFile.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dbd4/rails_model_file.rb', line 58 def initialize( = {}) init_variables @modelname = [:modelname] if [:modelname] if Inflector.singularize(@modelname) != @modelname raise RailsModelFileError, "ModelFile : modelname #{@modelname} is not valid, it must not be plural" end @modelfile = File.join('app', 'models', Inflector.underscore(Inflector.camelize(@modelname)) + ".rb") if ! FileTest.exist?(@modelfile) puts "Generating model #{@modelname}..." generate end load end end |
Instance Attribute Details
#modelfile=(value) ⇒ Object (writeonly)
Sets the attribute modelfile
30 31 32 |
# File 'lib/dbd4/rails_model_file.rb', line 30 def modelfile=(value) @modelfile = value end |
#modelname=(value) ⇒ Object (writeonly)
Sets the attribute modelname
30 31 32 |
# File 'lib/dbd4/rails_model_file.rb', line 30 def modelname=(value) @modelname = value end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
31 32 33 |
# File 'lib/dbd4/rails_model_file.rb', line 31 def warnings @warnings end |
Instance Method Details
#generate ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/dbd4/rails_model_file.rb', line 181 def generate if ! FileTest.exist?(@modelfile) if @comments =~ /SCAFFOLD/ cmdline = ['scaffold', @modelname] else cmdline = ['model', @modelname] end @infos << "Calling generator: #{cmdline.join(' ')}" Rails::Generator::Scripts::Generate.new.run(['model', @modelname]) if not $dryrun end end |
#init_variables ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dbd4/rails_model_file.rb', line 33 def init_variables @content = nil @lines = Array.new @warnings = Array.new @infos = Array.new @elements = { 'has_one' => {}, 'has_many' => {}, 'belongs_to' => {}, 'has_and_belongs_to_many' => {}, 'class' => {}, 'primary_key' => {}, 'table_name' => {}, 'as' => {}, 'acts_as_graph' => {} } end |
#load ⇒ Object
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 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/dbd4/rails_model_file.rb', line 76 def load raise "ModelFile : modelname not given" if ! @modelname raise "ModelFile : model file name not given" if ! @modelfile @content = IO.read(@modelfile) if ! @content @lines = IO.readlines(@modelfile) if @lines.empty? @original_lines = Array.new(@lines) raise "ModelFile : Cannot load, no content was given" if ! @content for i in (0..@lines.size-1) l = @lines[i] comment = false if l.match(/^\s*#/) #do nothing elsif l.match(/^\s*class\s+([A-Za-z_0-9]+)/) and @elements['class'].empty? @elements['class'] = { :start => i, :name => $~[1] } if @elements['class'][:name] != Inflector.camelize(@modelname) @warnings << "Warning in file #{@modelfile} at line #{i + 1}:" @warnings << " Class \"#{@elements['class'][:name]}\" does not match expected name : \"" + Inflector.camelize(@modelname) + "\"" @warnings << " Model name is \"#{@modelname}\" class name should be \"" + Inflector.camelize(@modelname) + "\"" end elsif l.match(/^\s*set_primary_key(?:[ \t\("'])+([a-zA-Z_0-9]+)/) if ! @elements['primary_key'].empty? @warnings << "Warning in file #{@modelfile} at line #{i + 1}:" @warnings << " Duplicate set_primary_key declaration, it is already defined at line #{@elements['primary_key'][:start] + 1}" @lines[i] = "#" + @lines[i] else @elements['primary_key'] = { :start => i, :name => $~[1] } end elsif l.match(/^\s*set_table_name(?:[ \t\("'])+([a-zA-Z_0-9]+)/) if ! @elements['table_name'].empty? @warnings << "Warning in file #{@modelfile} at line #{i + 1}:" @warnings << " Duplicate set_table_name declaration, it is already defined at line #{@elements['table_name'][:start] + 1}" @lines[i] = "#" + @lines[i] else @elements['table_name'] = { :start => i, :name => $~[1] } end elsif l.match(/^\s*acts_as_graph\(\{\}\)\s*$/) table_name = Inflector.pluralize(@modelname) if @elements['acts_as_graph'][""] @warnings << "Warning in file #{@modelfile} at line #{i + 1}:" @warnings << " Duplicate acts_as_graph declaration, it is already defined at line #{@elements['acts_as_graph'][table_name][:start] + 1}" @lines[i] = "#" + @lines[i] i += 1 while i < @lines.size break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/) @lines[i] = "#" + @lines[i] if @lines[i] !~ /^\s*#/ i += 1 end else r = @elements['acts_as_graph'][""] = { :start => i, :end => i } i += 1 while i < @lines.size break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/) r[:end] = i i += 1 end end elsif l.match(/^\s*(has_one|has_many|has_and_belongs_to_many|belongs_to)\s+:([a-zA-Z_0-9]+)/) r_type = $~[1] t_name = $~[2] if @elements[r_type][t_name] @warnings << "Warning in file #{@modelfile} at line #{i + 1}:" @warnings << " Duplicate declaration #{r_type}, it is already defined at line #{@elements[r_type][t_name][:start] + 1}" @lines[i] = "#" + @lines[i] i += 1 while i < @lines.size break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/) @lines[i] = "#" + @lines[i] if @lines[i] !~ /^\s*#/ i += 1 end else r = @elements[r_type][t_name] = { :start => i, :end => i } r[:foreign_key] = $~ if l.match(/:foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/) r[:polymorphic] = $~ if l.match(/:polymorphic\s*=>\s*true/) r[:join_table] = $~ if l.match(/:join_table\s*=>\s*["']([a-zA-Z_0-9]+)/) r[:as] = $~ if l.match(/:as\s*=>\s*:([a-zA-Z_0-9]+)/) r[:association_foreign_key] = $~ if l.match(/:association_foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/) i += 1 while i < @lines.size l2 = @lines[i] break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end)/) r[:foreign_key] = $~ if l2.match(/:foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/) r[:polymorphic] = $~ if l2.match(/:polymorphic\s*=>\s*true/) r[:as] = $~ if l2.match(/:as\s*=>\s*:([a-zA-Z_0-9]+)/) r[:join_table] = $~ if l2.match(/:join_table\s*=>\s*["']([a-zA-Z_0-9]+)/) r[:association_foreign_key] = $~ if l2.match(/:association_foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/) r[:end] = i i += 1 end end end end if @elements['class'].empty? @warnings << "Error : Could not find the class declaration in model file #{@modelfile}" raise "Error : Could not find the class declaration in model file #{@modelfile}" end end |
#setContent(string) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/dbd4/rails_model_file.rb', line 51 def setContent(string) init_variables @content = string @lines = string.readlines load end |
#to_str ⇒ Object
321 322 323 |
# File 'lib/dbd4/rails_model_file.rb', line 321 def to_str @lines.join("") end |
#update(table) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/dbd4/rails_model_file.rb', line 194 def update(table) raise "ModelFile : modelname not given" if ! @modelname raise "ModelFile : model file name not given" if ! @modelfile raise "ModelFile : no content given" if @content == nil left_over = @elements.deep_clone table.relation_starts.each_value do |r| dname = "" if r.type == :acts_as_graph dname = "" elsif r.type == :one2one dname = r.destination_table.modelname else dname = r.destination_table.name end etype = 'has_one' if r.type == :one2one etype = 'has_many' if r.type == :one2many etype = 'has_and_belongs_to_many' if r.type == :many2many etype = 'acts_as_graph' if r.type == :acts_as_graph element = @elements[etype][dname] #If the line exists if element if r.type == :acts_as_graph #do nothing, acts as graph can only be used with default parameters({}) elsif element[:as] if r.destination_column and r.destination_column.polymorphic for i in (element[:start] .. element[:end]) @lines[i].sub!(/(:as\s*=>\s*:)[a-zA-Z_0-9]+/, "\\1#{r.destination_column.polymorphic}") end else for i in (element[:start] .. element[:end]) @lines[i].sub!(/,\s*:as\s*=>\s*:[a-zA-Z_0-9]+/, "") end end else if r.destination_column and r.destination_column.polymorphic @lines[element[:start]].sub!(/(#{etype}[^,]+)/, "\\1, :as => '#{r.destination_column.polymorphic}'") end end left_over[etype].delete(dname) else if r.type == :acts_as_graph @lines[@elements['class'][:start]] += " #{etype}({})" else @lines[@elements['class'][:start]] += " #{etype} :#{dname}" end if r.destination_column and r.destination_column.polymorphic @lines[@elements['class'][:start]] += ", :as => :#{r.destination_column.polymorphic}" end @lines[@elements['class'][:start]] += "\n" end end table.relation_ends.each_value do |r| dname = "" if r.type == :many2many dname = r.source_table.name elsif r.type == :acts_as_graph next else if r.destination_column.polymorphic dname = r.destination_column.polymorphic else dname = r.source_table.modelname end end etype = 'belongs_to' if r.type == :one2one or r.type == :one2many etype = 'has_and_belongs_to_many' if r.type == :many2many element = @elements[etype][dname] #If the line exists if element if element[:polymorphic] if r.destination_column and ! r.destination_column.polymorphic for i in (element[:start] .. element[:end]) @lines[i].sub!(/,\s*:polymorphic\s*=>\s*["']?[a-zA-Z_0-9]+["']?/, "") end end else if r.destination_column and r.destination_column.polymorphic @lines[element[:start]].sub!(/(#{etype}[^,\n]+)/, "\\1, :polymorphic => true") else # do nothing end end left_over[etype].delete(dname) else @lines[@elements['class'][:start]] += " #{etype} :#{dname}" if r.destination_column and r.destination_column.polymorphic @elements[etype] = { dname => { :polymorphic => true, :added => true } } @lines[@elements['class'][:start]] += ", :polymorphic => true" end @lines[@elements['class'][:start]] += "\n" end end left_over.each do |k1, v1| next if k1 == 'class' or v1.empty? v1.each do |k2, v2| @warnings << "Warning in file #{@modelfile} at line #{v2[:start]}" @warnings << " #{k1} :#{k2} declaration is useless, will comment it" for i in (v2[:start] .. v2[:end]) @lines[i] = "#" + @lines[i] end end end # If we made a change to the document, generate it original_document_modified = @lines <=> @original_lines if (original_document_modified != 0) if ($dryrun == true) puts "Would of updated model #{@modelname} (file : #{@modelfile}) with content : " puts @lines else puts "Updating model #{@modelname} (file : #{@modelfile})..." newFile = File.open(@modelfile, "w") newFile.puts @lines end end end |