Class: Documatic::Component

Inherits:
Object
  • Object
show all
Includes:
OpenDocumentSpreadsheet::Helper, OpenDocumentText::Helper, ERB::Util
Defined in:
lib/documatic/component.rb

Constant Summary

Constants included from OpenDocumentSpreadsheet::Helper

OpenDocumentSpreadsheet::Helper::TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OpenDocumentSpreadsheet::Helper

#cell

Methods included from OpenDocumentText::Helper

#line_break, #para, #partial, #span

Constructor Details

#initialize(erb_text) ⇒ Component

Returns a new instance of Component.



12
13
14
15
# File 'lib/documatic/component.rb', line 12

def initialize(erb_text)
  @erb_text = erb_text
  @erb = ERB.new(erb_text)
end

Instance Attribute Details

#erbObject

Returns the value of attribute erb.



9
10
11
# File 'lib/documatic/component.rb', line 9

def erb
  @erb
end

#erb_textObject

Returns the value of attribute erb_text.



10
11
12
# File 'lib/documatic/component.rb', line 10

def erb_text
  @erb_text
end

Instance Method Details

#merge_partial_stylesObject

Merge the auto-styles from the cached partials into <office:automatic-styles> of this component. This method isn’t intended to be called directly by client applications: it is called automatically by Documatic::Template after processing.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/documatic/component.rb', line 61

def merge_partial_styles
  cache = Documatic::OpenDocumentText::Partial.cache_by_prefix
  if cache.length > 0
    styles = self.xml.root.elements['office:automatic-styles']
    if styles
      cache.each_value do |partial|
        partial.styles.each_element do |e|
          styles << e
        end
      end
    end
  end
end

#process(local_assigns) ⇒ Object

Injects the provided assigns into this component and sends it through ERB.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/documatic/component.rb', line 18

def process(local_assigns)
  if local_assigns.is_a? Binding
    context = local_assigns
  else  # Hash
    local_assigns.each do |key, val|
      self.define_singleton_method(key) do val end
    end
    context = binding
  end

  begin
    @xml = nil ; @text = self.erb.result(context)
  rescue
    lines = self.erb_text.split /\n/
    counter = 1
    lines.each do |line|
      puts "#{counter}:\t#{line}"
      counter += 1
    end
    raise
  end
end

#textObject Also known as: to_s

Returns the text of this component. Note that this flushes self.xml: subsequently if self.xml is called it will be reconstructed from this text and self.text will be flushed.



53
54
55
# File 'lib/documatic/component.rb', line 53

def text
  @text ||= ( remove_instance_variable(:@xml) ).to_s
end

#xmlObject

Returns a REXML::Document constructed from the text of this component. Note that this flushes self.text: subsequently if self.text is called it will be reconstructed from this XML document and self.xml will be flushed. Therefore the content of this partial is always stored either as XML or text, never both.



46
47
48
# File 'lib/documatic/component.rb', line 46

def xml
  @xml ||= REXML::Document.new( remove_instance_variable(:@text) )
end