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



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

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



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

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.



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

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



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

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