Class: ODFWriter::PathFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/odf_writer/path_finder.rb

Overview

PathFinder: find all fields and set name

Constant Summary collapse

IGNORE =

constants

{
  :fields    => /\A_/, 
  :texts     => /\A_/, 
  :bookmarks => /\A_/, 
  :images    => /\A_/, 
  :tables    => /\A_|\ATable/i,
  :sections  => /\A_|\ASection/i
}.freeze

Class Method Summary collapse

Class Method Details

.trail(node, leaf, options = {}) ⇒ Object

trail: find path in odt document, whereby only sections and tabes are searched as ancestors

node: Nokogiri node
leaf: hash, f.i. {:fields => ["NAME", "STREET", "ZIP", "PLACE"]}

options:
          :root   => :content | :styles, defaults to :root
          :paths  => :auto vivifying-hash, defaults to it


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/odf_writer/path_finder.rb', line 58

def trail( node, leaf, options={})
 
 # ignore
 ignore = IGNORE.merge(options[:ignore].to_h)
 
 # determine root
 root  = options[:root]  || :root
 
 # create auto-vivifying hash
 paths = options[:paths] || Hash.new { |h, k| h[k] = Hash.new(&h.default_proc)  }
 
 # for tables and sections add level with node name
 ancestors = node.ancestors.reverse.select{|ancestor| %w(section table).include?(ancestor.name)}
 
 path = [:files, root] + ancestors.map do |ancestor|
   filter_node(ancestor, ignore) 
 end.flatten #map
 
 # add each field in a nested hash
 paths.dig(*path)[leaf.keys.first]  = paths.dig(*path)[leaf.keys.first].presence || [] # cannot do '||=''
 paths.dig(*path)[leaf.keys.first] += filter_leafs(leaf, ignore)
 paths
end