Class: Regenerate::PageObject

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/regenerate/web-page.rb

Overview

The Ruby object contained within a web page. Instance variables defined in the HTML belong to this object, and Ruby code defined in the page is executed in the context of this object. This class is the base class for classes that define particular types of web pages

Instance Method Summary collapse

Methods included from Utils

#ensureDirectoryExists, #makeBackupFile

Instance Method Details

#erb(templateFileName) ⇒ Object

Method to render an ERB template file in the context of this object



595
596
597
598
599
600
601
602
603
# File 'lib/regenerate/web-page.rb', line 595

def erb(templateFileName)
  @binding = binding
  File.open(relative_path(templateFileName), "r") do |input|
    templateText = input.read
    template = ERB.new(templateText, nil, nil)
    template.filename = templateFileName
    result = template.result(@binding)
  end
end

#erbFromString(templateString) ⇒ Object

Method to render an ERB template (defined in-line) in the context of this object



606
607
608
609
610
# File 'lib/regenerate/web-page.rb', line 606

def erbFromString(templateString)
  @binding = binding
  template = ERB.new(templateString, nil, nil)
  template.result(@binding)
end

#relative_path(path) ⇒ Object

Calculate absolute path given path relative to the directory containing the source file for the web page



613
614
615
# File 'lib/regenerate/web-page.rb', line 613

def relative_path(path)
  File.expand_path(File.join(@baseDir, path.to_str))
end

#require_relative(path) ⇒ Object

Require a Ruby file given a path relative to the web page source file.



618
619
620
# File 'lib/regenerate/web-page.rb', line 618

def require_relative(path)
  require relative_path(path)
end

#savePropertiesObject

Save some of the page object’s instance variable values to a file as JSON This method depends on the following defined in the actual page object class:

  • propertiesToSave instance method, to return an array of symbols

  • propertiesFileName class method, to return name of properties file as a function of the web page source file name

(propertiesFileName is a class method, because it needs to be invoked by other code that _reads_ the properties
 file when the page object itself does not exist)

Example of useage: an index file for a blog needs to read properties of each blog page, where the blog page objects have saved their details into the individual property files.



630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/regenerate/web-page.rb', line 630

def saveProperties
  properties = {}
  for property in propertiesToSave
    value = instance_variable_get("@" + property.to_s)
    properties[property] = value
  end
  propertiesFileName = relative_path(self.class.propertiesFileName(@baseFileName))
  #puts "Saving properties #{properties.inspect} to #{propertiesFileName}"
  ensureDirectoryExists(File.dirname(propertiesFileName))
  File.open(propertiesFileName,"w") do |f|
    f.write(JSON.pretty_generate(properties))
  end
end