Skip to content

Instantly share code, notes, and snippets.

@vicentereig
Created October 31, 2012 16:18
Show Gist options
  • Save vicentereig/3987999 to your computer and use it in GitHub Desktop.
Save vicentereig/3987999 to your computer and use it in GitHub Desktop.
Sketching an ODT template generator
M::Document.configure do |config|
config.placeholder_format = "%:placeholder%"
end
class CustomerDetails
include M::DocumentFragment
include ActionView::Helpers::NumberHelper
subs :name, type: M::Text
subs :last_name, type: M::Text
subs :phone, type: M::Text
def phone
number_to_phone(@phone)
end
end
class Document
include M::Document
document_template 'document_template.odt'
document_name :document_name
substitute :title # type: 'Text', with: 'title', placeholder: 'title'
subs :gender, type: M::Text, with: 'gender'
subs :invoices, type: M::Table
subs :tasks, type: M::OrderedList
subs :car_images, type: M::Image # self.car_images returns Enumerable
subs :main_image, type: M::Image # self.main_image does not return an Enumerable
subs :customer, type: CustomerDetails, prefix: false
def document_name
"document_#{self.id}"
end
end
doc = Document.new(gender: 'male', invoices: [Invoice.new], tasks: [Tasks.new], car_images: [Image.new])
doc.render # => generates XML
doc.package # => packages everything into an ODT file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment