Class: ActiveFedora::RDFDatastream

Inherits:
Datastream
  • Object
show all
Includes:
ActiveFedora::RDF::Indexing, ActiveTriples::NestedAttributes, ActiveTriples::Properties, ActiveTriples::Reflection, 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, #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



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

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

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

.resource_class(klass = nil) ⇒ Class

ActiveTriples::Resource and include ActiveFedora::Rdf::Persistence.

Parameters:

  • an (Class)

    object to set as the resource class, Must be a descendant of

Returns:

  • (Class)

    the object resource class



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 25

def resource_class(klass=nil)
  if klass
    raise ArgumentError, "#{self} already has a resource_class #{@resource_class}, cannot redefine it to #{klass}" if @resource_class and klass != @resource_class
    raise ArgumentError, "#{klass} must be a subclass of ActiveTriples::Resource" unless klass < ActiveTriples::Resource
  end
  
  @resource_class ||= begin
                        klass = Class.new(klass || ActiveTriples::Resource)
                        klass.send(:include, RDF::Persistence)
                        klass
                      end
end

Instance Method Details

#contentObject



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

def content
  serialize
end

#content=(new_content) ⇒ Object



54
55
56
57
58
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 54

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

#content_changed?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 60

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

#deserialize(data = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 119

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



66
67
68
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 66

def freeze
  @resource.freeze
end

#metadata?Boolean

Returns:

  • (Boolean)


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

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.



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

def resource
  @resource ||= begin
                  klass = self.class.resource_class
                  klass.properties.merge(self.class.properties).each do |prop, config|
                    klass.property(config.term, 
                                   predicate: config.predicate, 
                                   class_name: config.class_name, 
                                   multivalue: config.multivalue)
                  end
                  klass.accepts_nested_attributes_for(*nested_attributes_options.keys) unless nested_attributes_options.blank?
                  uri_stub = digital_object ? self.class.rdf_subject.call(self) : nil

                  r = klass.new(uri_stub)
                  r.datastream = self
                  r << deserialize
                  r
                end
end

#serialization_formatObject



135
136
137
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 135

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

#serializeObject



114
115
116
117
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 114

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.



102
103
104
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 102

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

#update_indexed_attributes(hash) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 106

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