Class: Macros4Cuke::Templating::Section

Inherits:
UnaryElement show all
Defined in:
lib/macros4cuke/templating/engine.rb

Overview

Base class used internally by the template engine.
Represents a section in a template, that is, a set of template elements for which its rendition depends on the value of a variable.

Direct Known Subclasses

ConditionalSection

Instance Attribute Summary collapse

Attributes inherited from UnaryElement

#name

Instance Method Summary collapse

Constructor Details

#initialize(aVarName) ⇒ Section

Returns a new instance of Section.

Parameters:

  • aVarName (String)

    The name of the placeholder from a template.



128
129
130
131
# File 'lib/macros4cuke/templating/engine.rb', line 128

def initialize(aVarName)
  super(aVarName)
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

The child elements of the section



125
126
127
# File 'lib/macros4cuke/templating/engine.rb', line 125

def children
  @children
end

Instance Method Details

#add_child(aChild) ⇒ Object

Add a child element as member of the section



135
136
137
# File 'lib/macros4cuke/templating/engine.rb', line 135

def add_child(aChild)
  children << aChild
end

#render(aContextObject, theLocals) ⇒ String

Render the placeholder given the passed arguments. This method has the same signature as the Engine#render method.

Returns:

  • (String)

    The text value assigned to the placeholder. Returns an empty string when no value is assigned to the placeholder.

Raises:

  • (NotImplementedError)


161
162
163
164
# File 'lib/macros4cuke/templating/engine.rb', line 161

def render(aContextObject, theLocals)
  msg = "Method Section.#{__method__} must be implemented in subclass."
  raise NotImplementedError, msg
end

#variablesArray

Retrieve all placeholder names that appear in the template.

Returns:

  • (Array)

    The list of placeholder names.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/macros4cuke/templating/engine.rb', line 141

def variables()
  all_vars = children.each_with_object([]) do |a_child, subResult|
    case a_child
      when Placeholder
        subResult << a_child.name
      when Section
        subResult.concat(a_child.variables)
      else
        # Do nothing
    end
  end
  
  return all_vars.flatten.uniq
end