Class: Sablon::Processor::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/sablon/processor/document.rb,
lib/sablon/processor/document/blocks.rb,
lib/sablon/processor/document/field_handlers.rb,
lib/sablon/processor/document/operation_construction.rb

Overview

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

Defined Under Namespace

Classes: Block, CommentHandler, ConditionalHandler, EachLoopHandler, FieldHandler, ImageBlock, ImageHandler, InlineParagraphBlock, InsertionHandler, OperationConstruction, ParagraphBlock, RowBlock

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Document

Returns a new instance of Document.



68
69
70
# File 'lib/sablon/processor/document.rb', line 68

def initialize(parser)
  @parser = parser
end

Class Method Details

.default_field_handlerObject



54
55
56
# File 'lib/sablon/processor/document.rb', line 54

def default_field_handler
  @default_field_handler ||= nil
end

.field_handlersObject



50
51
52
# File 'lib/sablon/processor/document.rb', line 50

def field_handlers
  @field_handlers ||= {}
end

.parserObject



64
65
66
# File 'lib/sablon/processor/document.rb', line 64

def self.parser
  @parser ||= Sablon::Parser::MailMerge.new
end

.process(xml_node, env) ⇒ Object



59
60
61
62
# 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) ⇒ Object

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.



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

Removes a handler from the hash and returns it



33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object

Replaces an existing handler



45
46
47
48
# File 'lib/sablon/processor/document.rb', line 45

def replace_field_handler(name, handler)
  remove_field_handler(name)
  register_field_handler(name, handler)
end

Instance Method Details

#manipulate(xml_node, env) ⇒ Object



72
73
74
75
76
77
78
79
# 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