class Docxtor::Document::Element

Attributes

elements[RW]
xml[RW]

Public Class Methods

map(mappings) click to toggle source
# File lib/docxtor/document/element.rb, line 6
def self.map(mappings)
  mappings.each do |name, klass|
    define_method(name) do |*args, &block|
      elements << klass.new(*args, &block)
    end
  end
end
new(*args, &block) click to toggle source
# File lib/docxtor/document/element.rb, line 18
def initialize(*args, &block)
  @elements = []

  # FIXME You have to call #create_params in this hook
  after_initialize(*args)

  instance_eval &block if block_given?
end

Public Instance Methods

method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/docxtor/document/element.rb, line 14
def method_missing name, *args
  super unless [:header, :footer].include? name
end
render(xml) click to toggle source
# File lib/docxtor/document/element.rb, line 27
def render(xml)
  @xml = xml
end

Protected Instance Methods

after_initialize(*args;) click to toggle source
# File lib/docxtor/document/element.rb, line 33
def after_initialize *args; end
aliases() click to toggle source
# File lib/docxtor/document/element.rb, line 39
def aliases
  {}
end
properties() click to toggle source
# File lib/docxtor/document/element.rb, line 35
def properties
  {}
end
write_element(name, &block) click to toggle source
# File lib/docxtor/document/element.rb, line 47
def write_element(name, &block)
  xml.tag!("w:#{name}") do
    write_properties(name)
    instance_eval &block if block_given?
    write_elements
  end
end
write_elements() click to toggle source
# File lib/docxtor/document/element.rb, line 43
def write_elements
  @elements.each { |el| el.render(@xml) }
end
write_properties(el) click to toggle source
# File lib/docxtor/document/element.rb, line 55
def write_properties(el)
  @properties = get_properties_for(el)
  if @properties
    xml.tag!("w:#{el}Pr") do
      @properties.each { |k, v| write_property(k, v) }
    end
  end
end
write_property(key, val) click to toggle source
# File lib/docxtor/document/element.rb, line 64
def write_property(key, val)
  if self_closing? val
    xml.tag!("w:#{key}")
  elsif val
    if val.is_a?(Hash) && !val.empty?
      xml.tag!("w:#{key}", prefixize(val))
    else
      xml.tag!("w:#{key}", 'w:val' => val)
    end
  end
end

Private Instance Methods

create_params(options) click to toggle source
# File lib/docxtor/document/element.rb, line 78
def create_params(options)
  @params = Hash[options.map {|k,v| [aliases[k] || k, v]}]
end
get_properties_for(el) click to toggle source
# File lib/docxtor/document/element.rb, line 86
def get_properties_for(el)
  props = properties[el]
  if props
    pairs = @params.map do |k, v|
      unless props[k].nil?
        props[k].is_a?(Hash) ? [props[k][:name], v] : [props[k], v]
      end
    end
    Hash[pairs]
  end
end
prefixize(attrs) click to toggle source
# File lib/docxtor/document/element.rb, line 98
def prefixize(attrs)
  Hash[attrs.map { |k, v| ["w:#{k}", v] }]
end
self_closing?(val) click to toggle source
# File lib/docxtor/document/element.rb, line 82
def self_closing?(val)
  val == true
end