Class: Babylon::Base::View

Inherits:
Object
  • Object
show all
Defined in:
lib/babylon/base/view.rb

Overview

Your application’s views (stanzas) should be descendant of this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "", assigns = {}) ⇒ View

Instantiate a new view with the various varibales passed in assigns and the path of the template to render.



25
26
27
28
29
30
31
32
# File 'lib/babylon/base/view.rb', line 25

def initialize(path = "", assigns = {}) 
  @view_template = path 
  
  assigns.each do |key, value| 
    # Babylon.logger.debug("assigning @#{key}='#{value}'")
    instance_variable_set(:"@#{key}", value) 
  end 
end

Instance Attribute Details

#view_templateObject (readonly)

Returns the value of attribute view_template.



9
10
11
# File 'lib/babylon/base/view.rb', line 9

def view_template
  @view_template
end

Instance Method Details

#evaluateObject

“Loads” the view file, and uses the Nokogiri Builder to build the XML stanzas that will be sent.

Raises:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/babylon/base/view.rb', line 36

def evaluate 
  return if @view_template == ""
  
  # Babylon.logger.debug("eval with template #{@view_template} - looking thru #{Babylon.views}")
  raise ViewFileNotFound, "No such file #{@view_template}" unless Babylon.views[@view_template]
  builder = Nokogiri::XML::Builder.new 
  builder.stream do |xml|
    eval(Babylon.views[@view_template], binding, @view_template, 1)
  end
  builder.doc.root.children # we output the document built 
end

#render(xml, options = {}) ⇒ Object

Used to ‘include’ another view inside an existing view. The caller needs to pass the context in which the partial will be rendered Render must be called with :partial as well (other options will be supported later). The partial vale should be a relative path to another file view, from the calling view.

Raises:



16
17
18
19
20
21
# File 'lib/babylon/base/view.rb', line 16

def render(xml, options = {})
  # First, we need to identify the partial file path, based on the @view_template path.
  partial_path = (@view_template.split("/")[0..-2] + options[:partial].split("/")).join("/").gsub(".xml.builder", "") + ".xml.builder"
  raise ViewFileNotFound, "No such file #{partial_path}" unless Babylon.views[partial_path] 
  eval(Babylon.views[partial_path], binding, partial_path, 1)
end