class Caracal::Document

Public Class Methods

new(name = nil, &block) click to toggle source

This method instantiates a new word document.

# File lib/caracal/document.rb, line 103
def initialize(name = nil, &block)
  file_name(name)

  page_size
  page_margins top: 1440, bottom: 1440, left: 1440, right: 1440
  page_numbers

  [:font, :list_style, :namespace, :relationship, :style].each do |method|
    collection = self.class.send("default_#{ method }s")
    collection.each do |item|
      send(method, item)
    end
  end

  if block_given?
    (block.arity < 1) ? instance_eval(&block) : block[self]
  end
end
render(f_name = nil, &block) click to toggle source

This method renders a new Word document and returns it as a a string.

# File lib/caracal/document.rb, line 76
def self.render(f_name = nil, &block)
  docx   = new(f_name, &block)
  buffer = docx.render

  buffer.rewind
  buffer.sysread
end
save(f_name = nil, &block) click to toggle source

This method renders a new Word document and saves it to the file system.

# File lib/caracal/document.rb, line 87
def self.save(f_name = nil, &block)
  docx   = new(f_name, &block)
  docx.save
  # buffer = docx.render
  #
  # File.open(docx.path, 'wb') { |f| f.write(buffer.string) }
end

Public Instance Methods

contents() click to toggle source

This method returns an array of models which constitute the set of instructions for producing the document content.

# File lib/caracal/document.rb, line 128
def contents
  @contents ||= []
end
render() click to toggle source

This method renders the word document instance into a string buffer. Order is important!

# File lib/caracal/document.rb, line 138
def render
  buffer = ::Zip::OutputStream.write_buffer do |zip|
    render_package_relationships(zip)
    render_content_types(zip)
    render_app(zip)
    render_core(zip)
    render_custom(zip)
    render_fonts(zip)
    render_footer(zip)
    render_settings(zip)
    render_styles(zip)
    render_document(zip)
    render_relationships(zip)   # Must go here: Depends on document renderer
    render_media(zip)           # Must go here: Depends on document renderer
    render_numbering(zip)       # Must go here: Depends on document renderer
  end
end
save() click to toggle source
SAVING ==================================
# File lib/caracal/document.rb, line 159
def save
  buffer = render

  File.open(path, 'wb') { |f| f.write(buffer.string) }
end

Private Instance Methods

render_app(zip) click to toggle source
RENDERERS ===============================
# File lib/caracal/document.rb, line 173
def render_app(zip)
  content = ::Caracal::Renderers::AppRenderer.render(self)

  zip.put_next_entry('docProps/app.xml')
  zip.write(content)
end
render_content_types(zip) click to toggle source
# File lib/caracal/document.rb, line 180
def render_content_types(zip)
  content = ::Caracal::Renderers::ContentTypesRenderer.render(self)

  zip.put_next_entry('[Content_Types].xml')
  zip.write(content)
end
render_core(zip) click to toggle source
# File lib/caracal/document.rb, line 187
def render_core(zip)
  content = ::Caracal::Renderers::CoreRenderer.render(self)

  zip.put_next_entry('docProps/core.xml')
  zip.write(content)
end
render_custom(zip) click to toggle source
# File lib/caracal/document.rb, line 194
def render_custom(zip)
  content = ::Caracal::Renderers::CustomRenderer.render(self)

  zip.put_next_entry('docProps/custom.xml')
  zip.write(content)
end
render_document(zip) click to toggle source
# File lib/caracal/document.rb, line 201
def render_document(zip)
  content = ::Caracal::Renderers::DocumentRenderer.render(self)

  zip.put_next_entry('word/document.xml')
  zip.write(content)
end
render_fonts(zip) click to toggle source
# File lib/caracal/document.rb, line 208
def render_fonts(zip)
  content = ::Caracal::Renderers::FontsRenderer.render(self)

  zip.put_next_entry('word/fontTable.xml')
  zip.write(content)
end
render_media(zip) click to toggle source
# File lib/caracal/document.rb, line 222
def render_media(zip)
  images = relationships.select { |r| r.relationship_type == :image }
  images.each do |rel|
    if rel.relationship_data.to_s.size > 0
      content = rel.relationship_data
    else
      content = open(rel.relationship_target).read
    end

    zip.put_next_entry("word/#{ rel.formatted_target }")
    zip.write(content)
  end
end
render_numbering(zip) click to toggle source
# File lib/caracal/document.rb, line 236
def render_numbering(zip)
  content = ::Caracal::Renderers::NumberingRenderer.render(self)

  zip.put_next_entry('word/numbering.xml')
  zip.write(content)
end
render_package_relationships(zip) click to toggle source
# File lib/caracal/document.rb, line 243
def render_package_relationships(zip)
  content = ::Caracal::Renderers::PackageRelationshipsRenderer.render(self)

  zip.put_next_entry('_rels/.rels')
  zip.write(content)
end
render_relationships(zip) click to toggle source
# File lib/caracal/document.rb, line 250
def render_relationships(zip)
  content = ::Caracal::Renderers::RelationshipsRenderer.render(self)

  zip.put_next_entry('word/_rels/document.xml.rels')
  zip.write(content)
end
render_settings(zip) click to toggle source
# File lib/caracal/document.rb, line 257
def render_settings(zip)
  content = ::Caracal::Renderers::SettingsRenderer.render(self)

  zip.put_next_entry('word/settings.xml')
  zip.write(content)
end
render_styles(zip) click to toggle source
# File lib/caracal/document.rb, line 264
def render_styles(zip)
  content = ::Caracal::Renderers::StylesRenderer.render(self)

  zip.put_next_entry('word/styles.xml')
  zip.write(content)
end