module RDF::RDFObjects::Resource
Attributes
graph[R]
Public Instance Methods
[](p)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 21 def [](p) p = RDF::Resource.new(p) if p.is_a?(String) results = {} if p.is_a?(RDF::Resource) if p == RDF.to_rdf regex = Regexp.new(p.to_s) if assertions assertions.each_pair do |pred, objects| results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/ end end results.extend(AssertionSet) results.vocabulary = p results.subject = self else @graph.query(:subject=>self, :predicate=>p).each do |r| if r.object.is_a?(RDF::Resource) unless r.object.frozen? r.object.extend(RDF::RDFObjects::Resource) r.object.graph = self.graph end end results[r.predicate] ||=[] results[r.predicate] << r.object end end elsif p.is_a?(Class) && p.inspect =~ /^RDF::Vocabulary\(/ regex = Regexp.new(p.to_s) if assertions assertions.each_pair do |pred, objects| results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/ end end results.extend(AssertionSet) results.vocabulary = p results.subject = self elsif (RDF.const_defined?(p) && RDF.const_get(p).ancestors.index(RDF::Vocabulary)) regex = Regexp.new(RDF.const_get(p).to_s) if assertions assertions.each_pair do |pred, objects| results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/ end end results.extend(AssertionSet) results.vocabulary = RDF.const_get(p) results.subject = self end if results.is_a?(AssertionSet) results.each_pair do |pred, objs| objs.extend(ObjectSet) objs.set_statement(RDF::Statement.new(self, pred, "")) end obj_set = results else obj_set = case results.keys.length when 0 set = [] set.extend(ObjectSet) set.set_statement(RDF::Statement.new(self,p,"")) set when 1 set = [] results.values.first.each do |v| set << v end set.extend(ObjectSet) set.set_statement(RDF::Statement.new(self,p,"")) set end end obj_set end
[]=(p,o)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 96 def []=(p,o) p = RDF::URI.intern(p) unless p.is_a?(RDF::URI) @graph.query(:subject=>self, :predicate=>p).each {|stmt| @graph.delete(stmt)} [*o].each do |obj| next unless obj obj = cast_as_typed_object(obj) if obj.is_a?(RDF::Resource) && !obj.frozen? obj.graph = @graph elsif obj.is_a?(RDF::Resource) && obj.frozen? && obj.graph != @graph obj = RDF::URI.new(obj) obj.graph = @graph obj.freeze end @graph << [self, p, obj] unless obj.nil? end self[p] end
assert(p, o)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 8 def assert(p, o) p = RDF::URI.intern(p) unless p.is_a?(RDF::URI) unless o.is_a?(RDF::Resource) || o.is_a?(RDF::Literal) o = RDF::Literal.new(o) end @graph << [self, p, o] end
cast_as_typed_object(o)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 140 def cast_as_typed_object(o) return o if o.is_a?(RDF::Literal) || o.is_a?(RDF::Resource) return Literal.new(o) end
clear()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 202 def clear @graph.query(:subject=>self).each {|stmt| @graph.delete(stmt)} end
graph=(graph)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 4 def graph=(graph) @graph = graph end
method_missing(meth, *args)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 206 def method_missing(meth, *args) if RDF.const_defined?(meth) && RDF.const_get(meth).ancestors.index(RDF::Vocabulary) send(:"[]", meth) end end
object_of()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 157 def object_of subjects = [] @graph.query(:object=>self).each do |stmt| s = stmt.subject s.extend(RDF::RDFObjects::Resource) s.graph = @graph subjects << s end subjects end
predicates()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 145 def predicates p = [] @graph.query(:subject=>self).each {|stmt| p << stmt.predicate unless p.include?(stmt.predicate)} p end
properties()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 168 def properties results = {} @graph.query(:subject=>self).each do |r| if r.object.is_a?(RDF::Resource) r.object.graph = self.graph unless r.object.frozen? end results[r.predicate] ||=[] results[r.predicate] << r.object end obj_set = case results.keys.length when 0 then nil else results.each_pair do |pred, objs| #objs.each do |o| objs.extend(ObjectSet) objs.set_statement(RDF::Statement.new(self, pred, "")) #end #results[pred] = objs.first if objs.length == 1 results end end obj_set end
Also aliased as: assertions
push(p, o)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 134 def push(p, o) p = RDF::URI.intern(p) unless p.is_a?(RDF::URI) o = cast_as_typed_object(o) @graph << [self, p, o] end
relate(p, o)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 16 def relate(p, o) o = RDF::Resource.new(o) unless o.is_a?(RDF::Resource) assert(p, o) end
remove(p, o=nil)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 114 def remove(p, o=nil) p = RDF::URI.intern(p) unless p.is_a?(RDF::URI) removals = [] if o o = cast_as_typed_object(o) query = {:subject=>self, :predicate=>p, :object=>o} else query = {:subject=>self, :predicate=>p} end @graph.query(query).each do |stmt| removals << stmt @graph.delete(stmt) end if o removals.first else removals end end
statements()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 151 def statements stmts = [] @graph.query(:subject=>self, :predicate=>p).each {|stmt| stmts << stmt} stmts end
to_ntriples()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 212 def to_ntriples RDF::Writer.for(:ntriples).buffer do |writer| @graph.query(:subject=>self).each_statement do |statement| writer << statement end end end
type()
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 198 def type self[RDF.type] end
type=(rdf_type)
click to toggle source
# File lib/rdf/rdfobjects/resource.rb, line 194 def type=(rdf_type) self[RDF.type] = rdf_type end