Class: Cacofonix::Writer
- Inherits:
-
Object
- Object
- Cacofonix::Writer
- Defined in:
- lib/cacofonix/core/writer.rb
Overview
The primary way to write a new ONIX file.
Here’s a quick example. The generated file will be nice and boring, as the header and product objects have no data in them, but you get the idea.
File.open("output.xml","w") do |output|
header = Cacofonix::Header.new
Cacofonix::Writer.open(output, header) do |writer|
writer << Cacofonix::Product.new
writer << Cacofonix::Product.new
end
end
If you prefer, you can build your products using the APAProduct shim layer.
File.open("output.xml","w") do |output|
header = Cacofonix::Header.new
Cacofonix::Writer.open(output, header) do |writer|
writer << Cacofonix::APAProduct.new
writer << Cacofonix::APAProduct.new
end
end
You can also have the writer build and yield the product to you:
Cacofonix::Writer.open(output, header, :class => Cacofonix::APAProduct) do |writer|
writer.product do |product|
product.title = "Grimm's Fairy Tales"
product.publication_date = Date.today
end
writer.product do |product|
product.title = "Telling the Truth"
product.publication_date = Date.today - 7
end
end
… which then allows you to use “interpretation modules”:
module MySetters
def set_publication_date_to_xmas
self.publication_date = Date.parse("2001-12-25")
end
end
Cacofonix::Writer.open(output, header, :interpret => MySetters) do |writer|
writer.product { |product| product.set_publication_date_to_xmas }
end
Constant Summary collapse
- DOCTYPE =
"http://www.editeur.org/onix/2.1/03/reference/onix-international.dtd"
Class Method Summary collapse
Instance Method Summary collapse
- #<<(product) ⇒ Object
- #end_document ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(output, header, options = {}) ⇒ Writer
constructor
Default constructor.
- #product {|product| ... } ⇒ Object
Constructor Details
#initialize(output, header, options = {}) ⇒ Writer
Default constructor.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cacofonix/core/writer.rb', line 59 def initialize(output, header, = {}) raise ArgumentError, 'msg must be an Cacofonix::Header object' unless header.kind_of?(Cacofonix::Header) @output = output @header = header @options = @product_klass = @options[:class] || Cacofonix::Product @finished = false start_document end |
Class Method Details
.open(output, header, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/cacofonix/core/writer.rb', line 102 def self.open(output, header, = {}) if block_given? writer = self.new(output, header, ) yield writer writer.end_document else self.new(output, header, ) end end |
Instance Method Details
#<<(product) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/cacofonix/core/writer.rb', line 75 def << (product) unless product.kind_of?(Cacofonix::Product) || product.kind_of?(Cacofonix::SimpleProduct) raise ArgumentError, 'product must be an Cacofonix::Product or Cacofonix::SimpleProduct' end raise "Can't add products to a finished writer" if finished? @output.write(product.to_xml.to_s) @output.write("\n") end |
#end_document ⇒ Object
93 94 95 96 |
# File 'lib/cacofonix/core/writer.rb', line 93 def end_document @output.write("</ONIXMessage>\n") @finished = true end |
#finished? ⇒ Boolean
98 99 100 |
# File 'lib/cacofonix/core/writer.rb', line 98 def finished? @finished end |
#product {|product| ... } ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/cacofonix/core/writer.rb', line 85 def product raise ArgumentError, "Block required" unless block_given? product = @product_klass.new product.interpret @options[:interpret] yield product self << product end |