Class: Documatic::Component

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(erb_text) ⇒ Component

Returns a new instance of Component.



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

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

Instance Attribute Details

#erbObject

Returns the value of attribute erb.



7
8
9
# File 'lib/documatic/component.rb', line 7

def erb
  @erb
end

#erb_textObject

Returns the value of attribute erb_text.



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

def erb_text
  @erb_text
end

Instance Method Details

#merge_partial_styles(partials) ⇒ Object

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/documatic/component.rb', line 59

def merge_partial_styles(partials)
  styles = self.xml.root.elements['office:automatic-styles']
  if styles
    partials.each do |partial|
      partial.styles.each_element do |e|
        styles << e
      end
    end
  end
end

#process(local_assigns) ⇒ Object

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



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

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.



51
52
53
# File 'lib/documatic/component.rb', line 51

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.



44
45
46
# File 'lib/documatic/component.rb', line 44

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