Class: DirectedEdge::Exporter
- Inherits:
-
Object
- Object
- DirectedEdge::Exporter
- Defined in:
- lib/directed_edge.rb
Overview
A very simple class for creating Directed Edge XML files or doing batch updates to a database. This can be done for example with:
exporter = DirectedEdge::Exporter.new('mydatabase.xml')
item = DirectedEdge::Item.new(exporter.database, 'product_1')
item.add_tag('product')
exporter.export(item)
exporter.finish
mydatabase.xml
now contains:
<?xml version="1.0" encoding="UTF-8"?>
<directededge version="0.1">
<item id='product_1'><tag>product</tag></item>
</directededge>
Which can then be imported to a database on the server with:
database = DirectedEdge::Database.new('mydatabase', 'mypassword')
database.import('mydatabase.xml')
Alternatively, had the first line been:
exporter = DirectedEdge::Exporter.new(some_database_object)
Then newly created / modfied objects that on which export was called would be queued for a batch update to the database later.
Items may also be exported from existing databases.
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Provides a dummy database for use when creating new items to be exported.
Instance Method Summary collapse
-
#export(item) ⇒ Object
Exports the given item to the file passed to the constructor.
-
#finish ⇒ Object
Writes a closing XML element to the document and closes the file.
-
#initialize(destination) ⇒ Exporter
constructor
Begins exporting a collection of items to the given destination.
Constructor Details
#initialize(destination) ⇒ Exporter
Begins exporting a collection of items to the given destination. If the destination is a file existing contents will be overwritten. If the destination is an existing database object, updates will be queued until finish is called, at which point they will be uploaded to the webservices in batch.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/directed_edge.rb', line 240 def initialize(destination) if destination.is_a?(String) @database = Database.new('exporter') @file = File.new(destination, 'w') elsif destination.is_a?(Database) @database = destination @data = "" else raise TypeError.new("Exporter must be passed a file name or database object.") end write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n") write("<directededge version=\"0.1\">\n") end |
Instance Attribute Details
#database ⇒ Object (readonly)
Provides a dummy database for use when creating new items to be exported.
232 233 234 |
# File 'lib/directed_edge.rb', line 232 def database @database end |
Instance Method Details
#export(item) ⇒ Object
Exports the given item to the file passed to the constructor.
257 258 259 |
# File 'lib/directed_edge.rb', line 257 def export(item) write("#{item.to_xml}\n") end |
#finish ⇒ Object
Writes a closing XML element to the document and closes the file.
263 264 265 266 267 268 269 270 |
# File 'lib/directed_edge.rb', line 263 def finish write("</directededge>\n") if !@file.nil? @file.close else @database.resource['add'].put(@data) end end |