Class: Macros4Cuke::Templating::ConditionalSection

Inherits:
Section show all
Defined in:
lib/macros4cuke/templating/section.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.



68
69
70
71
# File 'lib/macros4cuke/templating/section.rb', line 68

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 non-existence (false).



63
64
65
# File 'lib/macros4cuke/templating/section.rb', line 63

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/macros4cuke/templating/section.rb', line 77

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.



92
93
94
# File 'lib/macros4cuke/templating/section.rb', line 92

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