class Sablon::Processor::Document

This class manages processing of the XML portions of a word document that can contain mailmerge fields

Public Class Methods

default_field_handler() click to toggle source
# File lib/sablon/processor/document.rb, line 54
def default_field_handler
  @default_field_handler ||= nil
end
field_handlers() click to toggle source
# File lib/sablon/processor/document.rb, line 50
def field_handlers
  @field_handlers ||= {}
end
new(parser) click to toggle source
# File lib/sablon/processor/document.rb, line 68
def initialize(parser)
  @parser = parser
end
parser() click to toggle source
# File lib/sablon/processor/document.rb, line 64
def self.parser
  @parser ||= Sablon::Parser::MailMerge.new
end
process(xml_node, env) click to toggle source
# File lib/sablon/processor/document.rb, line 59
def self.process(xml_node, env)
  processor = new(parser)
  processor.manipulate xml_node, env
end
register_field_handler(name, handler) click to toggle source

Adds a new handler to the OperationConstruction class. The handler passed in should be an instance of the Handler class or implement the same interface. Handlers cannot be replaced by this method, instead the `replace_field_handler` method should be used which internally removes the existing hander and registers the one passed in. The name 'default' is special and will be called if no other handlers can use the provided field.

# File lib/sablon/processor/document.rb, line 18
def register_field_handler(name, handler)
  name = name.to_sym
  if field_handlers[name] || (name == :default && !default_field_handler.nil?)
    msg = "Handler named: '#{name}' already exists. Use `replace_field_handler` instead."
    raise ArgumentError, msg
  end
  #
  if name == :default
    @default_field_handler = handler
  else
    field_handlers[name] = handler
  end
end
remove_field_handler(name) click to toggle source

Removes a handler from the hash and returns it

# File lib/sablon/processor/document.rb, line 33
def remove_field_handler(name)
  name = name.to_sym
  if name == :default
    handler = @default_field_handler
    @default_field_handler = nil
    handler
  else
    field_handlers.delete(name)
  end
end
replace_field_handler(name, handler) click to toggle source

Replaces an existing handler

# File lib/sablon/processor/document.rb, line 45
def replace_field_handler(name, handler)
  remove_field_handler(name)
  register_field_handler(name, handler)
end

Public Instance Methods

manipulate(xml_node, env) click to toggle source
# File lib/sablon/processor/document.rb, line 72
def manipulate(xml_node, env)
  operations = build_operations(@parser.parse_fields(xml_node))
  operations.each do |step|
    step.evaluate env
  end
  cleanup(xml_node)
  xml_node
end

Private Instance Methods

build_operations(fields) click to toggle source
# File lib/sablon/processor/document.rb, line 83
def build_operations(fields)
  OperationConstruction.new(fields,
                            self.class.field_handlers.values,
                            self.class.default_field_handler).operations
end
cleanup(xml_node) click to toggle source
# File lib/sablon/processor/document.rb, line 89
def cleanup(xml_node)
  fill_empty_table_cells xml_node
end
fill_empty_table_cells(xml_node) click to toggle source
# File lib/sablon/processor/document.rb, line 93
def fill_empty_table_cells(xml_node)
  selector = "//w:tc[count(*[name() = 'w:p'])=0 or not(*)]"
  xml_node.xpath(selector).each do |blank_cell|
    filler = Nokogiri::XML::Node.new('w:p', xml_node.document)
    blank_cell.add_child filler
  end
end