Class | SemanticRecord::Base |
In: |
lib/semantic_record/base.rb
|
Parent: | Object |
base | [RW] | |
connection | [RW] | |
rdf_type | [RW] | |
uri | [R] | |
uri | [RW] |
# File lib/semantic_record/base.rb, line 106 106: def self.base 107: @base ||= "http://example.org/" 108: end
# File lib/semantic_record/base.rb, line 118 118: def self.find 119: # if self isn't an inherited form of this 120: # class then return all existing instances 121: if self == SemanticRecord 122: s = "?nil" 123: else 124: uri = URI.parse self.uri #"#{self.base}#{self}" 125: if uri.absolute && uri.path 126: s = "<#{uri.to_s}>" 127: else 128: raise ArgumentError, "base uri seems to be invalid" 129: end 130: end 131: instances_response = TripleManager.get_subjects(s) 132: 133: end
# File lib/semantic_record/base.rb, line 114 114: def self.find_by_sparql(query) 115: TripleManager.get_by_sparql(query,true) 116: end
# File lib/semantic_record/base.rb, line 89 89: def self.inherited(receiver) 90: receiver.base = self.base 91: receiver.rdf_type = "http://http://www.w3.org/2000/01/rdf-schema#Class" 92: end
# File lib/semantic_record/base.rb, line 14 14: def initialize(uri) 15: @uri = uri 16: @presaved_attributes = {} 17: 18: TripleManager.describe(uri) 19: 20: if self.new_record? 21: self.rdf_type = self.class 22: end 23: end
# File lib/semantic_record/base.rb, line 98 98: def self.rdf_type 99: @rdf_type ||= "http://www.w3.org/2002/07/owl#Thing" 100: end
# File lib/semantic_record/base.rb, line 102 102: def self.uri 103: @uri ||= "#{self.base}#{self}" 104: end
# File lib/semantic_record/base.rb, line 35 35: def attribute(p_uri) 36: TripleManager.property_for(uri,p_uri) 37: end
# File lib/semantic_record/base.rb, line 39 39: def attributes 40: TripleManager.properties_for(uri) 41: end
# File lib/semantic_record/base.rb, line 43 43: def destroy! 44: remove = 45: "<transaction> 46: <remove> 47: <uri>#{uri}</uri> 48: <null/> 49: <null/> 50: </remove> 51: </transaction>" 52: connection.add!(remove, "application/x-rdftransaction") 53: end
# File lib/semantic_record/base.rb, line 81 81: def method_missing(mth,*args) 82: if mth.id2name.end_with?("=") 83: proxy_setter(mth,*args) 84: else 85: proxy_getter(mth,*args) 86: end 87: end
# File lib/semantic_record/base.rb, line 25 25: def new_record? 26: exists = TripleManager.exists_as_subject?(self.uri) 27: 28: !exists 29: end
# File lib/semantic_record/base.rb, line 55 55: def save 56: if new_record? 57: begin 58: @presaved_attributes.each {|predicate,value| 59: value.each do |val| 60: TripleManager.add(uri,predicate,val) 61: end 62: 63: @presaved_attributes.delete(predicate) 64: } 65: rescue 66: return false 67: end 68: else 69: begin 70: 71: TripleManager.update(uri,@presaved_attributes) 72: @presaved_attributes = {} 73: rescue ArgumentError 74: puts $! 75: return false 76: end 77: end 78: true 79: end
# File lib/semantic_record/base.rb, line 154 154: def expand(name) 155: ns, predicate = name.id2name.split("_",2) 156: if predicate.blank? 157: raise Namespaces::NoPredicateError, "no valid predicate defined" 158: end 159: namespace = Namespaces.resolve(ns) 160: 161: predicate = predicate.chomp("=") 162: 163: return namespace + predicate 164: end
# File lib/semantic_record/base.rb, line 142 142: def proxy_getter(mth,*args) 143: 144: predicate = mth.to_sym.expand#(mth) 145: 146: if @presaved_attributes.has_key?(predicate) 147: value_response = @presaved_attributes[predicate] 148: else 149: value_response = TripleManager.get_objects(uri,predicate,*args) 150: end 151: 152: end