Class: Caracal::Document
- Inherits:
-
Object
- Object
- Caracal::Document
- Includes:
- Core::FileName, Core::Fonts, Core::Images, Core::LineBreaks, Core::ListStyles, Core::Lists, Core::PageBreaks, Core::PageNumbers, Core::PageSettings, Core::Relationships, Core::Rules, Core::Styles, Core::Tables, Core::Text
- Defined in:
- lib/caracal/document.rb
Class Method Summary collapse
-
.render(f_name = nil, &block) ⇒ Object
This method renders a new Word document and returns it as a a string.
-
.save(f_name = nil, &block) ⇒ Object
This method renders a new Word document and saves it to the file system.
Instance Method Summary collapse
-
#contents ⇒ Object
This method returns an array of models which constitute the set of instructions for producing the document content.
-
#initialize(name = nil, &block) ⇒ Document
constructor
This method instantiates a new word document.
-
#render ⇒ Object
This method renders the word document instance into a string buffer.
Methods included from Core::Text
Methods included from Core::Tables
Methods included from Core::Rules
Methods included from Core::PageBreaks
Methods included from Core::Lists
Methods included from Core::LineBreaks
Methods included from Core::Images
Methods included from Core::ListStyles
Methods included from Core::Styles
Methods included from Core::PageNumbers
Methods included from Core::PageSettings
Methods included from Core::Fonts
Methods included from Core::Relationships
Methods included from Core::FileName
Constructor Details
#initialize(name = nil, &block) ⇒ Document
This method instantiates a new word document.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/caracal/document.rb', line 93 def initialize(name = nil, &block) file_name(name) page_size page_margins top: 1440, bottom: 1440, left: 1440, right: 1440 page_numbers [:relationship, :font, :style, :list_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.
67 68 69 70 71 72 73 |
# File 'lib/caracal/document.rb', line 67 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.
78 79 80 81 82 83 |
# File 'lib/caracal/document.rb', line 78 def self.save(f_name = nil, &block) docx = new(f_name, &block) buffer = docx.render File.open("./#{ docx.name }", 'w') { |f| f.write(buffer.string) } end |
Instance Method Details
#contents ⇒ Object
This method returns an array of models which constitute the set of instructions for producing the document content.
118 119 120 |
# File 'lib/caracal/document.rb', line 118 def contents @contents ||= [] end |
#render ⇒ Object
This method renders the word document instance into a string buffer. Order is important!
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/caracal/document.rb', line 128 def render buffer = ::Zip::OutputStream.write_buffer do |zip| render_package_relationships(zip) render_content_types(zip) render_app(zip) render_core(zip) render_fonts(zip) (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 |