Class: ActiveFedora::RDFDatastream
- Inherits:
-
Datastream
- Object
- Rubydora::Datastream
- Datastream
- ActiveFedora::RDFDatastream
- Includes:
- ModelMethods
- Defined in:
- lib/active_fedora/rdf_datastream.rb
Direct Known Subclasses
Defined Under Namespace
Modules: ModelMethods Classes: IndexObject, TermProxy
Instance Attribute Summary
Attributes inherited from Datastream
#digital_object, #dirty, #last_modified
Instance Method Summary (collapse)
-
- (Object) append(predicate, args)
append a value.
- - (Object) content=(*args)
-
- (Object) fields
returns a Hash, e.g.: => {:values => [], :type => :something, :behaviors => [], ...}.
- - (Object) find_predicate(predicate)
- - (Object) get_values(predicate)
- - (Object) graph
- - (Object) method_missing(name, *args)
-
- (Object) rdf_subject
Get the subject for this rdf/xml datastream.
- - (Object) serialization_format
-
- (Object) serialize
Creates a RDF datastream for insertion into a Fedora Object Note: This method is implemented on SemanticNode instead of RelsExtDatastream because SemanticNode contains the relationships array.
-
- (Object) serialize!
:nodoc:.
-
- (Object) set_value(predicate, args)
if there are any existing statements with this predicate, replace them.
-
- (Object) to_solr(solr_doc = Hash.new)
:nodoc:.
Methods inherited from Datastream
#add_ds_location, #add_mime_type, #create, #dirty?, from_xml, #initialize, #inspect, #new_object?, #profile_from_hash, #save, #size, #solrize_profile, #to_param, #validate_content_present
Constructor Details
This class inherits a constructor from ActiveFedora::Datastream
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name, *args)
257 258 259 260 261 262 263 264 265 |
# File 'lib/active_fedora/rdf_datastream.rb', line 257 def method_missing(name, *args) if (md = /^([^=]+)=$/.match(name.to_s)) && pred = find_predicate(md[1]) set_value(pred, *args) elsif pred = find_predicate(name) get_values(name) else super end end |
Instance Method Details
- (Object) append(predicate, args)
append a value
246 247 248 249 250 251 |
# File 'lib/active_fedora/rdf_datastream.rb', line 246 def append(predicate, args) predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI graph.add(predicate, args, true) graph.dirty = true return TermProxy.new(graph, predicate, args) end |
- (Object) content=(*args)
155 156 157 158 |
# File 'lib/active_fedora/rdf_datastream.rb', line 155 def content= *args @graph = nil super end |
- (Object) fields
returns a Hash, e.g.: => {:values => [], :type => :something, :behaviors => [], ...}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_fedora/rdf_datastream.rb', line 161 def fields field_map = {} graph.relationships.each do |predicate, values| vocab_sym, name = predicate.qname uri, vocab = self.class.vocabularies.select { |ns, v| v.__prefix__ == vocab_sym }.first next unless vocab config = self.class.config[:predicate_mapping][vocab.to_s] name, indexed_as = config.select { |k, v| name.to_s == v.to_s && k.to_s.split("__")[0] == self.class.prefix(name).to_s.split("__")[0]}.first next unless name and config.has_key?("#{name}type".to_sym) and config.has_key?("#{name}behaviors".to_sym) type = config["#{name}type".to_sym] behaviors = config["#{name}behaviors".to_sym] field_map[name.to_sym] = {:values => values.map {|v| v.to_s}, :type => type, :behaviors => behaviors} end field_map end |
- (Object) find_predicate(predicate)
196 197 198 199 200 |
# File 'lib/active_fedora/rdf_datastream.rb', line 196 def find_predicate(predicate) predicate = self.class.prefix(predicate) unless predicate.kind_of? RDF::URI result = ActiveFedora::Predicates.find_predicate(predicate) RDF::URI(result.reverse.join) end |
- (Object) get_values(predicate)
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/active_fedora/rdf_datastream.rb', line 220 def get_values(predicate) predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI results = graph[predicate] return if results.nil? values = [] results.each do |object| values << (object.kind_of?(RDF::Literal) ? object.value : object.to_s) end TermProxy.new(graph, predicate, values) end |
- (Object) graph
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/active_fedora/rdf_datastream.rb', line 202 def graph @graph ||= begin graph = RelationshipGraph.new unless new? RDF::Reader.for(serialization_format).new(content) do |reader| reader.each_statement do |statement| next unless statement.subject == rdf_subject literal = statement.object.kind_of?(RDF::Literal) object = literal ? statement.object.value : statement.object.to_s graph.add(statement.predicate, object, literal) end end end graph end end |
- (Object) rdf_subject
Get the subject for this rdf/xml datastream
269 270 271 |
# File 'lib/active_fedora/rdf_datastream.rb', line 269 def rdf_subject @subject ||= self.class.rdf_subject.call(self) end |
- (Object) serialization_format
253 254 255 |
# File 'lib/active_fedora/rdf_datastream.rb', line 253 def serialization_format raise "you must override the `serialization_format' method in a subclass" end |
- (Object) serialize
Creates a RDF datastream for insertion into a Fedora Object Note: This method is implemented on SemanticNode instead of RelsExtDatastream because SemanticNode contains the relationships array
277 278 279 280 281 282 283 284 |
# File 'lib/active_fedora/rdf_datastream.rb', line 277 def serialize out = RDF::Writer.for(serialization_format).buffer do |writer| graph.to_graph(rdf_subject).each_statement do |statement| writer << statement end end out end |
- (Object) serialize!
:nodoc:
149 150 151 152 153 |
# File 'lib/active_fedora/rdf_datastream.rb', line 149 def serialize! # :nodoc: if graph.dirty self.content = serialize end end |
- (Object) set_value(predicate, args)
if there are any existing statements with this predicate, replace them
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/active_fedora/rdf_datastream.rb', line 233 def set_value(predicate, args) predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI graph.delete(predicate) args = [args] unless args.respond_to? :each args.each do |arg| graph.add(predicate, arg, true) unless arg.empty? end graph.dirty = true return TermProxy.new(graph, predicate, args) end |
- (Object) to_solr(solr_doc = Hash.new)
:nodoc:
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/active_fedora/rdf_datastream.rb', line 179 def to_solr(solr_doc = Hash.new) # :nodoc: fields.each do |field_key, field_info| values = field_info.fetch(:values, false) if values field_info[:behaviors].each do |index_type| field_symbol = ActiveFedora::SolrService.solr_name(field_key, field_info[:type], index_type) values = [values] unless values.respond_to? :each values.each do |val| ::Solrizer::Extractor.insert_solr_field_value(solr_doc, field_symbol, val) end end end end solr_doc end |