Class: Macros4Cuke::Templating::ConditionalSection

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

Overview

Represents a section in a template, that is, a set of template elements for which its rendition depends on the (in)existence of an actual value bound to the variable name.

Instance Attribute Summary collapse

Attributes inherited from Section

#children

Attributes inherited from UnaryElement

#name

Instance Method Summary collapse

Methods inherited from Section

#add_child, #variables

Constructor Details

#initialize(aVarName, renderWhenExisting = true) ⇒ ConditionalSection

Returns a new instance of ConditionalSection.

Parameters:

  • aVarName (String)

    The name of the placeholder from a template.

  • renderWhenExisting (boolean) (defaults to: true)

    When true, render the children elements if a value exists for the variable.



173
174
175
176
# File 'lib/macros4cuke/templating/engine.rb', line 173

def initialize(aVarName, renderWhenExisting = true)
  super(aVarName)
  @existence = renderWhenExisting
end

Instance Attribute Details

#existenceObject (readonly)

A boolean that indicates whether the rendition condition is the existence of a value for the variable (true) or its inexistence (false).



169
170
171
# File 'lib/macros4cuke/templating/engine.rb', line 169

def existence
  @existence
end

Instance Method Details

#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.



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/macros4cuke/templating/engine.rb', line 183

def render(aContextObject, theLocals)
  actual_value = retrieve_value_from(aContextObject, theLocals)
  if (!actual_value.nil? && existence) || (actual_value.nil? && !existence)
    # Let render the children
    result = children.each_with_object('') do |a_child, sub_result|
      sub_result << a_child.render(aContextObject, theLocals)
    end
  else
    result = ''
  end
  
  return result
end

#to_sString

Returns The original text representation of the tag.

Returns:

  • (String)

    The original text representation of the tag.



199
200
201
# File 'lib/macros4cuke/templating/engine.rb', line 199

def to_s()
  return "<?#{name}>"
end