Class: ODFWriter::Document
- Inherits:
-
Object
- Object
- ODFWriter::Document
- Includes:
- Images
- Defined in:
- lib/odf_writer/document.rb
Overview
Document: create document from template
Constant Summary
Constants included from Images
Instance Method Summary collapse
-
#add_bookmark(file, name, opts = {}, &block) ⇒ Object
add_bookmark.
-
#add_bookmark_reader(file, name = nil) ⇒ Object
add_bookmark_reader.
-
#add_field(file, name, opts = {}, &block) ⇒ Object
add_field.
-
#add_field_reader(file, name = nil) ⇒ Object
add_field_reader.
-
#add_image(file, name, opts = {}, &block) ⇒ Object
add_image.
-
#add_image_reader(file, name = nil) ⇒ Object
add_image_reader.
-
#add_list_style(root, *list_styles) ⇒ Object
add_list_style.
-
#add_readers(files = ODFWriter::Template::CONTENT_FILES.keys) ⇒ Object
add_readers.
-
#add_section(file, name, collection, opts = {}) {|section| ... } ⇒ Object
add_section.
-
#add_section_reader(file, name = nil) ⇒ Object
add_section_reader.
-
#add_style(root, *styles) ⇒ Object
add_style.
-
#add_table(file, name, collection, opts = {}) {|table| ... } ⇒ Object
add_table.
-
#add_table_reader(file, name = nil) ⇒ Object
add_table_reader.
-
#add_text(file, name, opts = {}, &block) ⇒ Object
add_text.
-
#add_text_reader(file, name = nil) ⇒ Object
add_text_reader.
-
#initialize(path = nil, zip_stream: nil, &block) ⇒ Document
constructor
initialize.
-
#leafs(file, doc) ⇒ Object
leafs.
-
#populate(object, options = {}) ⇒ Object
populate.
-
#tree ⇒ Object
tree.
-
#write(**options) ⇒ Object
write.
Methods included from Images
#avoid_duplicate_image_names, #find_image_name_matches, #include_image_files
Constructor Details
#initialize(path = nil, zip_stream: nil, &block) ⇒ Document
initialize
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/odf_writer/document.rb', line 37 def initialize(path = nil, zip_stream: nil, &block) @template = ODFWriter::Template.new(path, :zip_stream => zip_stream) @fields = {} @field_readers = {} @texts = {} @text_readers = {} @bookmarks = {} @bookmark_readers = {} @images = {} @image_readers = {} @tables = {} @table_readers = {} @sections = {} @section_readers = {} @styles = {} @list_styles = {} if block_given? instance_eval(&block) end end |
Instance Method Details
#add_bookmark(file, name, opts = {}, &block) ⇒ Object
add_bookmark
127 128 129 130 |
# File 'lib/odf_writer/document.rb', line 127 def add_bookmark(file, name, opts={}, &block) opts.merge!(:name => name) @bookmarks[file] ||= []; @bookmarks[file] << Bookmark.new(opts, &block) end |
#add_bookmark_reader(file, name = nil) ⇒ Object
add_bookmark_reader
137 138 139 |
# File 'lib/odf_writer/document.rb', line 137 def add_bookmark_reader(file, name=nil) @bookmark_readers[file] ||= []; @bookmark_readers[file] << BookmarkReader.new(:name => name) end |
#add_field(file, name, opts = {}, &block) ⇒ Object
add_field
89 90 91 92 |
# File 'lib/odf_writer/document.rb', line 89 def add_field(file, name, opts={}, &block) opts.merge!(:name => name) @fields[file] ||= []; @fields[file] << Field.new(opts, &block) end |
#add_field_reader(file, name = nil) ⇒ Object
add_field_reader
99 100 101 |
# File 'lib/odf_writer/document.rb', line 99 def add_field_reader(file, name=nil) @field_readers[file] ||= []; @field_readers[file] << FieldReader.new(:name => name) end |
#add_image(file, name, opts = {}, &block) ⇒ Object
add_image
146 147 148 149 |
# File 'lib/odf_writer/document.rb', line 146 def add_image(file, name, opts={}, &block) opts.merge!(:name => name) @images[file] ||= []; @images[file] << Image.new(opts, &block) end |
#add_image_reader(file, name = nil) ⇒ Object
add_image_reader
156 157 158 |
# File 'lib/odf_writer/document.rb', line 156 def add_image_reader(file, name=nil) @image_readers[file] ||= []; @image_readers[file] << ImageReader.new(:name => name) end |
#add_list_style(root, *list_styles) ⇒ Object
add_list_style
218 219 220 221 222 |
# File 'lib/odf_writer/document.rb', line 218 def add_list_style(root, *list_styles ) list_styles.each do |list_style| @list_styles[root] ||= []; @list_styles[root] << ListStyle.new( list_style ) end end |
#add_readers(files = ODFWriter::Template::CONTENT_FILES.keys) ⇒ Object
add_readers
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/odf_writer/document.rb', line 73 def add_readers(files = ODFWriter::Template::CONTENT_FILES.keys) files.each do |file| add_bookmark_reader(file) add_field_reader(file) add_text_reader(file) add_image_reader(file) add_section_reader(file) add_table_reader(file) end end |
#add_section(file, name, collection, opts = {}) {|section| ... } ⇒ Object
add_section
186 187 188 189 190 191 |
# File 'lib/odf_writer/document.rb', line 186 def add_section(file, name, collection, opts={}) opts.merge!(:name => name, :collection => collection) section = Section.new(opts) @sections[file] ||= []; @sections[file] << section yield(section) end |
#add_section_reader(file, name = nil) ⇒ Object
add_section_reader
198 199 200 |
# File 'lib/odf_writer/document.rb', line 198 def add_section_reader(file, name=nil) @section_readers[file] ||= []; @section_readers[file] << SectionReader.new(:name => name) end |
#add_style(root, *styles) ⇒ Object
add_style
207 208 209 210 211 |
# File 'lib/odf_writer/document.rb', line 207 def add_style(root, *styles ) styles.each do |style| @styles[root] ||= []; @styles[root] << Style.new( style ) end end |
#add_table(file, name, collection, opts = {}) {|table| ... } ⇒ Object
add_table
165 166 167 168 169 170 |
# File 'lib/odf_writer/document.rb', line 165 def add_table(file, name, collection, opts={}) opts.merge!(:name => name, :collection => collection) table = Table.new(opts) @tables[file] ||= []; @tables[file] << table yield(table) end |
#add_table_reader(file, name = nil) ⇒ Object
add_table_reader
177 178 179 |
# File 'lib/odf_writer/document.rb', line 177 def add_table_reader(file, name=nil) @table_readers[file] ||= []; @table_readers[file] << TableReader.new(:name => name) end |
#add_text(file, name, opts = {}, &block) ⇒ Object
add_text
108 109 110 111 |
# File 'lib/odf_writer/document.rb', line 108 def add_text(file, name, opts={}, &block) opts.merge!(:name => name) @texts[file] ||= []; @texts[file] << Text.new(opts, &block) end |
#add_text_reader(file, name = nil) ⇒ Object
add_text_reader
118 119 120 |
# File 'lib/odf_writer/document.rb', line 118 def add_text_reader(file, name=nil) @text_readers[file] ||= []; @text_readers[file] << TextReader.new(:name => name) end |
#leafs(file, doc) ⇒ Object
leafs
243 244 245 246 247 248 249 250 |
# File 'lib/odf_writer/document.rb', line 243 def leafs( file, doc) results={} results = deep_merge( results, @bookmark_readers[file].map { |r| r.paths(file, doc) }.inject{|m,n| deep_merge(m, n)} ) if @bookmark_readers[file] results = deep_merge( results, @field_readers[file].map { |r| r.paths(file, doc) }.inject{|m,n| deep_merge(m, n)} ) if @field_readers[file] results = deep_merge( results, @text_readers[file].map { |r| r.paths(file, doc) }.inject{|m,n| deep_merge(m, n)} ) if @text_readers[file] results = deep_merge( results, @image_readers[file].map { |r| r.paths(file, doc) }.inject{|m,n| deep_merge(m, n)} ) if @image_readers[file] results end |
#populate(object, options = {}) ⇒ Object
populate
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/odf_writer/document.rb', line 257 def populate(object, ={}) file = [:file] || :content coll = [:coll] || [] tree = [:tree] || self.tree prok = [:proc] list = [:list]; list = Array(list).compact tree.each do |key, names| case key when :fields names.each do |name| add_field(file, name, .reverse_merge(:value => prok ? prok.call(object, name) : object.try(name.downcase.to_sym))) end #def when :texts names.each do |name| add_text(file, name, .reverse_merge(:value => prok ? prok.call(object, name) : object.try(name.downcase.to_sym))) end #def when :bookmarks names.each do |name| add_bookmark(file, name, .reverse_merge(:value => prok ? prok.call(object, name) : object.try(name.downcase.to_sym))) end #def when :images names.each do |name| add_image(file, name, .reverse_merge(:value => (prok ? prok.call(object, name) : object.try(name.downcase.to_sym)))) end #def when :tables names.each do |name, table_tree| if list.include?(name) add_table(file, name, coll, ){|table| table.populate(table_tree, )} elsif object.respond_to?(name.underscore.to_sym) add_table(file, name, arrify(object.send(name.underscore.to_sym)), ){|table| table.populate(table_tree, )} end end #def when :sections names.each do |name, section_tree| if list.include?(name) add_section(file, name, coll, ){|section| section.populate(section_tree, )} if list.include?(name) elsif object.respond_to?(name.underscore.to_sym) add_section(file, name, arrify(object.send(name.underscore.to_sym)), ){|section| section.populate(section_tree, )} end end #def when :files names.each do |file, file_tree| populate(object, .merge(:file => file, :tree => file_tree)) end end #case end #each end |
#tree ⇒ Object
tree
229 230 231 232 233 234 235 236 |
# File 'lib/odf_writer/document.rb', line 229 def tree results = {} @template.content do |file, doc| #results.deep_merge!( leafs( file, doc)) # requires Rails results = deep_merge(results, leafs( file, doc)) end results end |
#write(**options) ⇒ Object
write
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/odf_writer/document.rb', line 312 def write(**) dest = .delete(:dest) @template.update_content do |template| template.update_files do |file, doc, manifest| @styles[file].to_a.each { |s| s.add_document_style(doc) } @styles[file].to_a.each { |s| s.add_automatic_style(doc) } @list_styles[file].to_a.each { |s| s.add_list_style(doc) } @sections[file].to_a.each { |s| s.replace!(doc, manifest, template) } @tables[file].to_a.each { |t| t.replace!(doc, manifest, template) } @texts[file].to_a.each { |t| t.replace!(doc, ) } @fields[file].to_a.each { |f| f.replace!(doc, ) } @bookmarks[file].to_a.each { |b| b.replace!(doc) } @images[file].to_a.each { |i| i.replace!(doc, manifest, template) } Image.unique_image_names( doc ) if @images.present? end end if dest ::File.open(dest, "wb") {|f| f.write(@template.data) } else @template.data end end |