Class: ActiveFedora::RDFDatastream

Inherits:
Datastream
  • Object
show all
Includes:
RdfNode, Solrizer::Common
Defined in:
lib/active_fedora/rdf_datastream.rb

Direct Known Subclasses

NtriplesRDFDatastream, RdfxmlRDFDatastream

Instance Attribute Summary

Attributes inherited from Datastream

#digital_object, #last_modified

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RdfNode

#append, #config_for_term_or_uri, #delete_predicate, #find_predicate, #get_values, #method_missing, #query, #rdf_subject, #reset_rdf_subject!, #set_value, #target_class

Methods inherited from Datastream

#create, #dirty, #dirty=, #dirty?, from_xml, #initialize, #inspect, #new_object?, #profile_from_hash, #save, #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 in the class ActiveFedora::RdfNode

Class Method Details

.rdf_subject {|ds| ... } ⇒ Object

Register a ruby block that evaluates to the subject of the graph By default, the block returns the current object’s pid This should override the method in RdfObject, which just creates a b-node by default

Yields:

  • (ds)

    ‘ds’ is the datastream instance



20
21
22
23
24
25
26
# File 'lib/active_fedora/rdf_datastream.rb', line 20

def rdf_subject &block
  if block_given?
     return @subject_block = block
  end

  @subject_block ||= lambda { |ds| RDF::URI.new("info:fedora/#{ds.pid}") }
end

.register_vocabularies(*vocabs) ⇒ Object



28
29
30
# File 'lib/active_fedora/rdf_datastream.rb', line 28

def register_vocabularies(*vocabs)
  Deprecation.warn(RDFDatastream, "register_vocabularies no longer has any effect and will be removed in active-fedora 6.0", caller)
end

Instance Method Details

#contentObject

Overriding so that one can call ds.content on an unsaved datastream and they will see the serialized format



45
46
47
# File 'lib/active_fedora/rdf_datastream.rb', line 45

def content
  serialize
end

#content=(content) ⇒ Object



49
50
51
# File 'lib/active_fedora/rdf_datastream.rb', line 49

def content=(content)
  @graph = deserialize(content)
end

#content_changed?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/active_fedora/rdf_datastream.rb', line 53

def content_changed?
  @content = serialize
  super
end

#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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_fedora/rdf_datastream.rb', line 76

def deserialize(data = nil)
  repository = RDF::Repository.new
  return repository if new? and data.nil?

  data ||= datastream_content

  RDF::Reader.for(serialization_format).new(data) do |reader|
    reader.each_statement do |statement|
      repository << statement
    end
  end

  repository
end

#graphObject



91
92
93
94
95
# File 'lib/active_fedora/rdf_datastream.rb', line 91

def graph
  @graph ||= begin
    deserialize
  end      
end

#metadata?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_fedora/rdf_datastream.rb', line 34

def metadata?
  true
end

#prefix(name) ⇒ Object



38
39
40
41
42
# File 'lib/active_fedora/rdf_datastream.rb', line 38

def prefix(name)
  name = name.to_s unless name.is_a? String
  pre = dsid.underscore
  return "#{pre}__#{name}".to_sym
end

#serialization_formatObject



97
98
99
# File 'lib/active_fedora/rdf_datastream.rb', line 97

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



103
104
105
106
# File 'lib/active_fedora/rdf_datastream.rb', line 103

def serialize
  update_subjects_to_use_a_real_pid!
  RDF::Writer.for(serialization_format).dump(graph)
end

#to_solr(solr_doc = Hash.new) ⇒ Object

:nodoc:



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_fedora/rdf_datastream.rb', line 58

def to_solr(solr_doc = Hash.new) # :nodoc:
  fields.each do |field_key, field_info|
    values = get_values(rdf_subject, field_key)
    directive = Solrizer::Directive.new(field_info[:type], field_info[:behaviors])
    if values
      Array(values).each do |val|    
        val = val.to_s if val.kind_of? RDF::URI
        self.class.create_and_insert_terms(prefix(field_key), val, directive, solr_doc)
      end
    end
  end
  solr_doc
end