Class: Sheng::Sequence
Instance Attribute Summary
#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
#add_errors_from_node, #basic_node_elements, #basic_nodes, #initialize, #nodes, #required_hash, #to_tree
#checkbox_element_path, #mergefield_element_path, #new_mergefield_element_path
Constructor Details
This class inherits a constructor from Sheng::Block
Instance Method Details
#add_sequence_element(member, index, last: false) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# 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? ⇒ Boolean
17
18
19
20
|
# 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 ⇒ Object
44
45
46
|
# File 'lib/sheng/sequence.rb', line 44
def clean_up
[@start_field, @end_field, xml_fragment].map(&:remove)
end
|
35
36
37
38
39
40
41
42
|
# 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 ⇒ Object
48
49
50
51
52
|
# 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) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
|
# 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) ⇒ Object
58
59
60
61
62
63
64
|
# 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? ⇒ Boolean
54
55
56
|
# File 'lib/sheng/sequence.rb', line 54
def series_with_commas?
@start_field.series_with_commas?
end
|