Sha256: 405ed50d4864267935f45083ce10101587ab19bafb0db79862cd07706ac98ae3

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

module Docxtor2
  class Package::Document::Element < ElementList
    include BlockEvaluator
    include ObjectUtils

    def initialize(*args, &block)
      @params = create_attributes(args)
      @block = block
      super()
    end

    def render(xml)
      @xml = xml
      evaluate &@block
    end

    protected

    def mappings
      {}
    end

    def aliases
      {}
    end

    def write_element(name, &block)
      @xml.tag!("w:#{name}") do
        write_properties(name)
        evaluate &block
        write_elements(@xml)
      end
    end

    def write_properties(el)
      @properties = get_properties_for(el)
      unless @properties.nil?
        @xml.tag!("w:#{el}Pr") do
          @properties.each { |k, v| write_property(k, v) }
        end
      end
    end

    def write_property(key, val)
      if self_closing? val
        @xml.tag!("w:#{key}")
      elsif !val.nil?
        if val.is_a?(Hash) && !val.empty?
          @xml.tag!("w:#{key}", prefixize(val))
        elsif !val.to_s.empty?
          @xml.tag!("w:#{key}", 'w:val' => val)
        end
      end
    end

    private

    def create_attributes(args)
      hash = find_argument(args, Hash, {})
      pairs = hash.map do |k, v|
        aliases.key?(k) ? [aliases[k], v] : [k, v]
      end
      Hash[pairs]
    end

    def self_closing?(val)
      !!val == val && val
    end

    def get_properties_for(el)
      map = mappings[el]
      unless map.nil?
        pairs = @params.
          reject { |k, v| !map.key?(k) }.
          map { |k, v|
            element = map[k]
            element.is_a?(Hash) ?
              [element[:name], v] :
              [element, v]
          }
        Hash[pairs]
      end
    end

    def prefixize(attrs)
      Hash[attrs.map { |k, v| ["w:#{k}", v] }]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
docxtor2-0.0.9 lib/docxtor2/package/document/element.rb
docxtor2-0.1.0 lib/docxtor2/package/document/element.rb