Class: TaliaUtil::Xml::RdfBuilder

Inherits:
BaseBuilder show all
Defined in:
lib/talia_util/xml/rdf_builder.rb

Overview

Class for creating xml-rdf data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseBuilder

make_xml_string, open

Class Method Details

.open_for_triples(triples, options = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/talia_util/xml/rdf_builder.rb', line 28

def self.open_for_triples(triples, options = nil)
  my_builder = self.new(options)
  
  triple_hash = my_builder.send(:prepare_triples, triples)
  
  my_builder.send(:build_structure) do
    my_builder.send(:write_for_triples, triple_hash)
  end
end

.xml_string_for_triples(triples) ⇒ Object



39
40
41
42
43
# File 'lib/talia_util/xml/rdf_builder.rb', line 39

def self.xml_string_for_triples(triples)
  xml = ''
  open_for_triples(triples, :target => xml, :indent => 2)
  xml
end

Instance Method Details

#write_triple(subject, predicate, object) ⇒ Object

Writes a simple “flat” triple. If the object is a string, it will be treated as a “value” while an object (ActiveSource or N::URI) will be treated as a “link”.

Throws an exception if the predicate cannot be turned into a namespaced representation



12
13
14
15
16
17
18
# File 'lib/talia_util/xml/rdf_builder.rb', line 12

def write_triple(subject, predicate, object)
  subject = subject.respond_to?(:uri) ? subject.uri.to_s : subject
  predicate = predicate.respond_to?(:uri) ? predicate : N::URI.new(predicate) 
  @builder.rdf :Description, "rdf:about" => subject do
    write_predicate(predicate, [ object ])
  end
end

#write_triples(triples) ⇒ Object

Writes all the given triples.



21
22
23
24
25
# File 'lib/talia_util/xml/rdf_builder.rb', line 21

def write_triples(triples)
  triples.each do |triple|
    write_triple(*triple)
  end
end