Class: ActiveFedora::RDFDatastream

Inherits:
Datastream
  • Object
show all
Extended by:
ActiveTriples::Properties
Includes:
ActiveFedora::Rdf::Indexing, ActiveTriples::NestedAttributes, Solrizer::Common
Defined in:
lib/active_fedora/rdf/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 ActiveFedora::Rdf::Indexing

#apply_prefix, #prefix, #primary_solr_name, #to_solr

Methods inherited from Datastream

#create, #frozen?, #initialize, #inspect, #label, #profile_from_hash, #realLabel, #save, #serialize!, #solrize_profile, #to_param, #to_solr

Constructor Details

This class inherits a constructor from ActiveFedora::Datastream

Class Method Details

.rdf_subject(&block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 11

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

  @subject_block ||= lambda { |ds| ds.pid }
end

.resource_classObject

Utility method which can be overridden to determine the object resource that is created.



21
22
23
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 21

def resource_class
  Rdf::ObjectResource
end

Instance Method Details

#contentObject



37
38
39
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 37

def content
  serialize
end

#content=(new_content) ⇒ Object



41
42
43
44
45
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 41

def content=(new_content)
  resource.clear!
  resource << deserialize(new_content)
  new_content
end

#content_changed?Boolean



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

def content_changed?
  return false unless instance_variable_defined? :@resource
  @content = serialize
  super
end

#deserialize(data = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 101

def deserialize(data=nil)
  return RDF::Graph.new if new? && data.nil?

  if data.nil?
    data = datastream_content
  elsif behaves_like_io?(data)
    data = io_content(data)
  end

  # Because datastream_content can return nil, we should check that here.
  return RDF::Graph.new if data.nil?

  data.force_encoding('utf-8')
  RDF::Graph.new << RDF::Reader.for(serialization_format).new(data)
end

#freezeObject



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

def freeze
  @resource.freeze
end

#metadata?Boolean



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

def metadata?
  true
end

#resourceObject Also known as: graph

The resource is the RdfResource object that stores the graph for the datastream and is the central point for its relationship to other nodes.

set_value, get_value, and property accessors are delegated to this object.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 63

def resource
  @resource ||= begin
                  r = self.singleton_class.resource_class.new(digital_object ? self.class.rdf_subject.call(self) : nil)
                  r.singleton_class.properties = self.class.properties
                  r.singleton_class.properties.keys.each do |property|
                    r.singleton_class.send(:register_property, property)
                  end
                  r.datastream = self
                  r.singleton_class.accepts_nested_attributes_for(*nested_attributes_options.keys) unless nested_attributes_options.blank?
                  r << deserialize
                  r
                end
end

#serialization_formatObject



117
118
119
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 117

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

#serializeObject



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

def serialize
  resource.set_subject!(pid) if (digital_object or pid) and rdf_subject.node?
  resource.dump serialization_format
end

#term_values(*values) ⇒ Object

This method allows for delegation. This patches the fact that there's no consistent API for allowing delegation - we're matching the OMDatastream implementation as our “consistency” point. @TODO: We may need to enable deep RDF delegation at one point.



84
85
86
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 84

def term_values(*values)
  self.send(values.first)
end

#update_indexed_attributes(hash) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 88

def update_indexed_attributes(hash)
  hash.each do |fields, value|
    fields.each do |field|
      self.send("#{field}=", value)
    end
  end
end