class Sheng::Sequence

Public Instance Methods

add_sequence_element(member, index, last: false) click to toggle source
# File lib/sheng/sequence.rb, line 22
def add_sequence_element(member, index, last: false)
  if series_with_commas? && index > 0
    @start_field.add_previous_sibling(serial_comma_node(index, last: last))
  end
  new_node_set = @start_field.add_previous_sibling(dup_node_set)
  merge_field_set = MergeFieldSet.new("#{key}_#{index}", new_node_set)
  member = { @start_field.iteration_variable => member } unless member.is_a?(Hash)
  merge_field_set.interpolate(DataSet.new(member))
  if index == 0 || last
    copy_section_formatting(new_node_set, side: last ? "end" : "start")
  end
end
array_of_primitives_expected?() click to toggle source
# File lib/sheng/sequence.rb, line 17
def array_of_primitives_expected?
  return true if nodes.map(&:key).uniq == ["item"]
  @start_field.iteration_variable != :item
end
clean_up() click to toggle source
# File lib/sheng/sequence.rb, line 44
def clean_up
  [@start_field, @end_field, xml_fragment].map(&:remove)
end
copy_section_formatting(node_set, side:) click to toggle source
# File lib/sheng/sequence.rb, line 35
def copy_section_formatting(node_set, side:)
  field = instance_variable_get(:"@#{side}_field")
  if field.styling_paragraph && field.styling_paragraph.at_xpath(".//w:sectPr")
    existing_ppr = node_set.at_xpath(".//w:pPr")
    existing_ppr && existing_ppr.remove
    node_set.first.prepend_child(field.styling_paragraph.dup)
  end
end
dup_node_set() click to toggle source
# File lib/sheng/sequence.rb, line 48
def dup_node_set
  xml_fragment.each_with_object(Nokogiri::XML::NodeSet.new(xml_document)) do |child, dup_content|
    dup_content << child.dup
  end
end
interpolate(data_set) click to toggle source
# File lib/sheng/sequence.rb, line 5
def interpolate(data_set)
  collection = data_set.fetch(key)
  if collection.respond_to?(:each_with_index)
    collection.each_with_index do |item, i|
      add_sequence_element(item, i, last: i == collection.length - 1)
    end
    clean_up
  end
rescue DataSet::KeyNotFound
  nil
end
serial_comma_node(index, last: false) click to toggle source
# File lib/sheng/sequence.rb, line 58
def serial_comma_node(index, last: false)
  content = ", #{last ? "#{@start_field.comma_series_conjunction} " : ""}"
  content.gsub!(/\,/, '') if last && index == 1
  Sheng::Support.new_text_run(
    content, xml_document: xml_document, space_preserve: true
  )
end
series_with_commas?() click to toggle source
# File lib/sheng/sequence.rb, line 54
def series_with_commas?
  @start_field.series_with_commas?
end