Class: ActiveFedora::RDFDatastream

Inherits:
Datastream
  • Object
show all
Includes:
ModelMethods
Defined in:
lib/active_fedora/rdf_datastream.rb

Direct Known Subclasses

NtriplesRDFDatastream, RdfxmlRDFDatastream

Defined Under Namespace

Modules: ModelMethods Classes: IndexObject, TermProxy

Instance Attribute Summary collapse

Attributes inherited from Datastream

#digital_object, #last_modified

Instance Method Summary collapse

Methods inherited from Datastream

#create, #dirty, #dirty=, #dirty?, from_xml, #initialize, #inspect, #new_object?, #profile_from_hash, #serialize!, #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

#method_missing(name, *args) ⇒ Object



318
319
320
321
322
323
324
325
326
# File 'lib/active_fedora/rdf_datastream.rb', line 318

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 Attribute Details

#loadedObject

Returns the value of attribute loaded.



152
153
154
# File 'lib/active_fedora/rdf_datastream.rb', line 152

def loaded
  @loaded
end

Instance Method Details

#append(predicate, args) ⇒ Object

append a value

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



306
307
308
309
310
311
312
# File 'lib/active_fedora/rdf_datastream.rb', line 306

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

#changed?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/active_fedora/rdf_datastream.rb', line 272

def changed?
   graph_changed? || super
end

#contentObject



248
249
250
251
252
253
254
# File 'lib/active_fedora/rdf_datastream.rb', line 248

def content
  # return the RDF graph content if it's available (or all we have)
  return serialize if loaded or new?

  # otherwise, grab the data from the datastore
  super
end

#content=(content) ⇒ Object



164
165
166
167
168
# File 'lib/active_fedora/rdf_datastream.rb', line 164

def content=(content)
  content_will_change!
  self.loaded = true
  @graph = deserialize(content)
end

#datastream_contentObject

alias the original datastream content



246
# File 'lib/active_fedora/rdf_datastream.rb', line 246

alias :datastream_content :content

#deserialize(data = nil) ⇒ Object

Populate a RDFDatastream object based on the “datastream” content Assumes that the datastream contains RDF content

Parameters:

  • data (String) (defaults to: nil)

    the “rdf” node



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/active_fedora/rdf_datastream.rb', line 217

def deserialize(data = nil)
  graph = RelationshipGraph.new
  return graph if new? and data.nil?

  data ||= datastream_content

    RDF::Reader.for(serialization_format).new(data) do |reader|
      reader.each_statement do |statement|
        begin
          next unless statement.subject == rdf_subject
        rescue
        end
        literal = statement.object.kind_of?(RDF::Literal)
        object = literal ? statement.object.value : statement.object.to_s
        graph.add(statement.predicate, object, literal)
      end
    end

  graph
end

#fieldsObject

returns a Hash, e.g.: => {:values => [], :type => :something, :behaviors => [], …}



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/active_fedora/rdf_datastream.rb', line 171

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

#find_predicate(predicate) ⇒ Object

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



208
209
210
211
212
# File 'lib/active_fedora/rdf_datastream.rb', line 208

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

#get_values(predicate) ⇒ Object

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/active_fedora/rdf_datastream.rb', line 277

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

#graphObject



238
239
240
241
242
243
# File 'lib/active_fedora/rdf_datastream.rb', line 238

def graph
  @graph ||= begin
    self.loaded = true
    deserialize
  end
end

#graph_changed?Boolean

Returns:

  • (Boolean)


260
261
262
263
264
265
266
267
268
269
270
# File 'lib/active_fedora/rdf_datastream.rb', line 260

def graph_changed?
  return true if changed_attributes.has_key?('graph')
  return false unless loaded

  if new?
    !serialize.empty?
  else
    serialize.strip != (datastream_content || '').strip
  end

end

#graph_will_change!Object



256
257
258
# File 'lib/active_fedora/rdf_datastream.rb', line 256

def graph_will_change!
  changed_attributes['graph'] = nil
end

#metadata?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/active_fedora/rdf_datastream.rb', line 154

def metadata?
  true
end

#rdf_subjectObject

Get the subject for this rdf/xml datastream



330
331
332
# File 'lib/active_fedora/rdf_datastream.rb', line 330

def rdf_subject
  @subject ||= self.class.rdf_subject.call(self)
end

#saveObject



158
159
160
161
162
# File 'lib/active_fedora/rdf_datastream.rb', line 158

def save
  @content = serialize  if graph_changed? and content_will_change!

  super
end

#serialization_formatObject



314
315
316
# File 'lib/active_fedora/rdf_datastream.rb', line 314

def serialization_format
  raise "you must override the `serialization_format' method in a subclass"
end

#serializeObject

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



336
337
338
339
340
341
342
343
# File 'lib/active_fedora/rdf_datastream.rb', line 336

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

#set_value(predicate, args) ⇒ Object

if there are any existing statements with this predicate, replace them

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/active_fedora/rdf_datastream.rb', line 291

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|
    arg = arg.to_s if arg.kind_of? RDF::Literal
    graph.add(predicate, arg, true) unless arg.empty?
  end
  graph.dirty = true
  return TermProxy.new(graph, predicate, args)
end

#to_solr(solr_doc = Hash.new) ⇒ Object

:nodoc:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/active_fedora/rdf_datastream.rb', line 190

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