Class: Caracal::Document

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Header

included

Methods included from Core::Footer

included

Methods included from Core::Text

included

Methods included from Core::TableOfContents

included

Methods included from Core::Tables

included

Methods included from Core::Rules

included

Methods included from Core::PageBreaks

included

Methods included from Core::PageFlips

included

Methods included from Core::Lists

included

Methods included from Core::Images

included

Methods included from Core::IFrames

included

Methods included from Core::Fields

included

Methods included from Core::Bookmarks

included

Methods included from Core::ListStyles

included

Methods included from Core::Styles

included

Methods included from Core::PageNumbers

included

Methods included from Core::PageSettings

included

Methods included from Core::Fonts

included

Methods included from Core::Relationships

included

Methods included from Core::Namespaces

included

Methods included from Core::Ignorables

included

Methods included from Core::FileName

included

Methods included from Core::CustomProperties

included

Constructor Details

#initialize(name = nil, &block) ⇒ Document

This method instantiates a new word document.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/caracal/document.rb', line 114

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

Class Method Details

.render(f_name = nil, &block) ⇒ Object

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



87
88
89
90
91
92
93
# File 'lib/caracal/document.rb', line 87

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) ⇒ Object

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



98
99
100
101
102
103
104
# File 'lib/caracal/document.rb', line 98

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

Instance Method Details

#contentsObject

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



139
140
141
# File 'lib/caracal/document.rb', line 139

def contents
  @contents ||= []
end


143
144
145
# File 'lib/caracal/document.rb', line 143

def footer_content
  @footer_content
end

#header_contentObject



147
148
149
# File 'lib/caracal/document.rb', line 147

def header_content
  @header_content
end

#renderObject

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/caracal/document.rb', line 156

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_header(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

#saveObject

SAVING ==================================


178
179
180
181
182
# File 'lib/caracal/document.rb', line 178

def save
  buffer = render

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