Class: Regenerate::WebPage
- Inherits:
-
Object
- Object
- Regenerate::WebPage
show all
- Includes:
- Utils
- Defined in:
- lib/regenerate/web-page.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#ensureDirectoryExists, #makeBackupFile
Constructor Details
#initialize(fileName) ⇒ WebPage
Returns a new instance of WebPage.
241
242
243
244
245
246
247
248
249
|
# File 'lib/regenerate/web-page.rb', line 241
def initialize(fileName)
@fileName = fileName
@components = []
@currentComponent = nil
@componentInstanceVariables = {}
initializePageObject(PageObject.new)
@rubyComponents = []
readFileLines
end
|
Instance Attribute Details
#fileName ⇒ Object
Returns the value of attribute fileName.
239
240
241
|
# File 'lib/regenerate/web-page.rb', line 239
def fileName
@fileName
end
|
Instance Method Details
#addRubyComponent(rubyComponent) ⇒ Object
268
269
270
|
# File 'lib/regenerate/web-page.rb', line 268
def addRubyComponent(rubyComponent)
@rubyComponents << rubyComponent
end
|
#checkOutputFileUnchanged(outFile, oldFile) ⇒ Object
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'lib/regenerate/web-page.rb', line 375
def checkOutputFileUnchanged(outFile, oldFile)
if File.exists? oldFile
oldFileContents = File.read(oldFile)
newFileContents = File.read(outFile)
if oldFileContents != newFileContents
newFileName = outFile + ".new"
File.rename(outFile, newFileName)
File.rename(oldFile, outFile)
raise UnexpectedChangeError.new("New file #{newFileName} is different from old file #{outFile}: #{diffReport(newFileContents,oldFileContents)}")
end
else
raise UnexpectedChangeError.new("Can't check #{outFile} against backup #{oldFile} " +
"because backup file doesn't exist")
end
end
|
#classFromString(str) ⇒ Object
302
303
304
305
306
|
# File 'lib/regenerate/web-page.rb', line 302
def classFromString(str)
str.split('::').inject(Object) do |mod, class_name|
mod.const_get(class_name)
end
end
|
#diffReport(newString, oldString) ⇒ Object
362
363
364
365
366
367
368
369
370
371
372
373
|
# File 'lib/regenerate/web-page.rb', line 362
def diffReport(newString, oldString)
i = 0
minLength = [newString.length, oldString.length].min
while i<minLength and newString[i] == oldString[i] do
i += 1
end
diffPos = i
newStringEndPos = [diffPos+20,newString.length].min
oldStringEndPos = [diffPos+20, newString.length].min
startPos = [0, diffPos-10].max
"Different from position #{diffPos}: \n #{newString[startPos...newStringEndPos].inspect}\n !=\n #{oldString[startPos...newStringEndPos].inspect}"
end
|
#display ⇒ Object
456
457
458
459
460
461
462
463
|
# File 'lib/regenerate/web-page.rb', line 456
def display
puts "=========================================================================="
puts "Output of #{@fileName}"
for component in @components do
puts "--------------------------------------"
puts(component.output)
end
end
|
#executeRubyComponents ⇒ Object
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
# File 'lib/regenerate/web-page.rb', line 441
def executeRubyComponents
fileDir = File.dirname(@fileName)
puts "Executing ruby components in directory #{fileDir} ..."
Dir.chdir(fileDir) do
for rubyComponent in @rubyComponents
rubyCode = rubyComponent.text
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
puts "Executing ruby (line #{rubyComponent.lineNumber}) #{rubyCode.inspect} ..."
@pageObject.instance_eval(rubyCode, @fileName, rubyComponent.lineNumber)
end
end
end
|
#finish ⇒ Object
351
352
353
354
355
356
357
358
359
360
|
# File 'lib/regenerate/web-page.rb', line 351
def finish
if @currentComponent
if @currentComponent.is_a? StaticHtml
@currentComponent.finishText
@currentComponent = nil
else
raise ParseException.new("Unfinished last component at end of file")
end
end
end
|
#getPageObjectInstanceVar(varName) ⇒ Object
259
260
261
|
# File 'lib/regenerate/web-page.rb', line 259
def getPageObjectInstanceVar(varName)
@pageObject.instance_variable_get(varName)
end
|
#initializePageObject(pageObject) ⇒ Object
251
252
253
254
255
256
257
|
# File 'lib/regenerate/web-page.rb', line 251
def initializePageObject(pageObject)
@pageObject = pageObject
setPageObjectInstanceVar("@fileName", @fileName)
setPageObjectInstanceVar("@baseDir", File.dirname(@fileName))
setPageObjectInstanceVar("@baseFileName", File.basename(@fileName))
@initialInstanceVariables = Set.new(@pageObject.instance_variables)
end
|
#processCommandLine(parsedCommandLine, lineNumber) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/regenerate/web-page.rb', line 313
def processCommandLine(parsedCommandLine, lineNumber)
if @currentComponent && (@currentComponent.is_a? StaticHtml)
@currentComponent.finishText
@currentComponent = nil
end
if @currentComponent
if parsedCommandLine.sectionStart
raise ParseException.new("Unexpected section start #{parsedCommandLine} inside component")
end
@currentComponent.(parsedCommandLine)
@currentComponent = nil
else
if !parsedCommandLine.sectionStart
raise ParseException.new("Unexpected section end #{parsedCommandLine}, outside of component")
end
if parsedCommandLine.isInstanceVar
if parsedCommandLine.
startNewComponent(HtmlVariable.new, parsedCommandLine)
else
startNewComponent(.new, parsedCommandLine)
end
else
if parsedCommandLine.name == "ruby"
startNewComponent(RubyCode.new(lineNumber+1), parsedCommandLine)
elsif parsedCommandLine.name == "class"
startNewComponent(SetPageObjectClass.new(parsedCommandLine.value), parsedCommandLine)
else
raise ParseException.new("Unknown section type #{parsedCommandLine.name.inspect}")
end
end
if @currentComponent.finished
@currentComponent = nil
end
end
end
|
#processTextLine(line, lineNumber) ⇒ Object
294
295
296
297
298
299
300
|
# File 'lib/regenerate/web-page.rb', line 294
def processTextLine(line, lineNumber)
if @currentComponent == nil
startNewComponent(StaticHtml.new)
end
@currentComponent.addLine(line)
end
|
#readFileLines ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
# File 'lib/regenerate/web-page.rb', line 405
def readFileLines
puts "Opening #{@fileName} ..."
lineNumber = 0
File.open(@fileName).each_line do |line|
line.chomp!
lineNumber += 1
= .match(line)
if
parsedCommandLine = .new(line, )
if parsedCommandLine.
parsedCommandLine.checkIsValid
processCommandLine(parsedCommandLine, lineNumber)
else
processTextLine(line, lineNumber)
end
else
processTextLine(line, lineNumber)
end
end
finish
end
|
#regenerate ⇒ Object
430
431
432
433
434
|
# File 'lib/regenerate/web-page.rb', line 430
def regenerate
executeRubyComponents
writeRegeneratedFile(@fileName)
end
|
#regenerateToOutputFile(outFile, checkNoChanges = false) ⇒ Object
436
437
438
439
|
# File 'lib/regenerate/web-page.rb', line 436
def regenerateToOutputFile(outFile, checkNoChanges = false)
executeRubyComponents
writeRegeneratedFile(outFile, checkNoChanges)
end
|
#setInstanceVarValue(varName, value) ⇒ Object
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/regenerate/web-page.rb', line 272
def setInstanceVarValue(varName, value)
if @initialInstanceVariables.member? varName
raise Exception, "Instance variable #{varName} is a pre-existing instance variable"
end
if @componentInstanceVariables.member? varName
raise Exception, "Instance variable #{varName} is a already defined for a component"
end
instance_variable_set(varName, value)
componentInstanceVariables << varName
end
|
#setPageObject(className) ⇒ Object
308
309
310
311
|
# File 'lib/regenerate/web-page.rb', line 308
def setPageObject(className)
pageObjectClass = classFromString(className)
initializePageObject(pageObjectClass.new)
end
|
#setPageObjectInstanceVar(varName, value) ⇒ Object
263
264
265
266
|
# File 'lib/regenerate/web-page.rb', line 263
def setPageObjectInstanceVar(varName, value)
puts " setPageObjectInstanceVar, #{varName} = #{value.inspect}"
@pageObject.instance_variable_set(varName, value)
end
|
#startNewComponent(component, startComment = nil) ⇒ Object
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/regenerate/web-page.rb', line 283
def startNewComponent(component, = nil)
component.parentPage = self
@currentComponent = component
@components << component
if
component.()
end
end
|
#writeRegeneratedFile(outFile, checkNoChanges) ⇒ Object
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/regenerate/web-page.rb', line 391
def writeRegeneratedFile(outFile, checkNoChanges)
backupFileName = makeBackupFile(outFile)
puts "Outputting regenerated page to #{outFile} ..."
File.open(outFile, "w") do |f|
for component in @components do
f.write(component.output)
end
end
puts "Finished writing #{outFile}"
if checkNoChanges
checkOutputFileUnchanged(outFile, backupFileName)
end
end
|