Class: Sheng::ConditionalBlock

Inherits:
Block show all
Defined in:
lib/sheng/conditional_block.rb

Instance Attribute Summary

Attributes inherited from MergeFieldSet

#errors, #key, #xml_document, #xml_fragment

Instance Method Summary collapse

Methods inherited from Block

#extract_mergefields, #get_node_set_and_end_field, #initialize, #raw_key

Methods inherited from MergeFieldSet

#add_errors_from_node, #basic_node_elements, #basic_nodes, #initialize, #nodes, #required_hash, #to_tree

Methods included from PathHelpers

#checkbox_element_path, #mergefield_element_path, #new_mergefield_element_path

Constructor Details

This class inherits a constructor from Sheng::Block

Instance Method Details

#conditional_typeObject



17
18
19
# File 'lib/sheng/conditional_block.rb', line 17

def conditional_type
  @start_field.block_prefix
end

#criterion_met?(variable) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/sheng/conditional_block.rb', line 21

def criterion_met?(variable)
  variable_exists = variable && (variable == true || !variable.empty?)
  if conditional_type == "if"
    variable_exists
  else
    !variable_exists
  end
end

#interpolate(data_set) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/sheng/conditional_block.rb', line 5

def interpolate(data_set)
  variable = data_set.fetch(key, :default => nil)
  @start_field.remove
  @end_field.remove
  if criterion_met?(variable)
    merge_field_set = MergeFieldSet.new("#{conditional_type}_#{key}", xml_fragment)
    merge_field_set.interpolate(data_set)
  else
    xml_fragment.remove
  end
end