Class: Macros4Cuke::Templating::ConditionalSection

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

Overview

A specialized section in a template 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.



212
213
214
215
# File 'lib/macros4cuke/templating/engine.rb', line 212

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



207
208
209
# File 'lib/macros4cuke/templating/engine.rb', line 207

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.



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/macros4cuke/templating/engine.rb', line 223

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.



239
240
241
# File 'lib/macros4cuke/templating/engine.rb', line 239

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