Class: ActiveFedora::RDFDatastream

Inherits:
File
  • Object
show all
Includes:
ActiveFedora::RDF::DatastreamIndexing, ActiveTriples::NestedAttributes, ActiveTriples::Properties, ActiveTriples::Reflection
Defined in:
lib/active_fedora/rdf/rdf_datastream.rb

Direct Known Subclasses

NtriplesRDFDatastream, RDFXMLDatastream

Constant Summary

Constants included from AttributeMethods

AttributeMethods::AttrNames

Instance Attribute Summary

Attributes included from File::Attributes

#mime_type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveFedora::RDF::DatastreamIndexing

#primary_solr_name, #to_solr

Methods inherited from File

#==, #attribute_will_change!, #changed?, #check_fixity, #datastream_will_change!, #described_by, #exists!, #frozen?, #initialize, #inspect, #ldp_connection, #ldp_source, #metadata, #new_record?, #readonly?, #refresh, #reload, #remote_content, #serialize!, #to_solr

Methods included from Querying

#default_sort_params, extended

Methods included from Identifiable

#id, #id=, #pid, #uri

Methods included from AttributeMethods

#[], #[]=, #attribute_names, #attributes

Methods included from Versionable

#create_version, #has_versions?, #model_type, #restore_version, #versions

Methods included from Persistence

#delete, #destroy, #destroyed?, #eradicate, #new_record?, #persisted?, #save, #save!, #update

Methods included from File::Streaming

#headers, #stream

Methods included from File::Attributes

#digest, #dirty_size, #empty?, #has_content?, #original_name, #original_name=, #persisted_size, #size

Constructor Details

This class inherits a constructor from ActiveFedora::File

Class Method Details

.add_attribute_indexing_config(name, &block) ⇒ Object



24
25
26
27
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 24

def add_attribute_indexing_config(name, &block)
  Deprecation.warn(RDFDatastream, "Adding indexing to datastreams is deprecated")
  index_config[name] ||= ActiveFedora::Indexing::Map::IndexObject.new(name, &block)
end

.parent_uri(ds) ⇒ Object

Trim the last segment off the URI to get the parents uri



30
31
32
33
34
35
36
37
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 30

def parent_uri(ds)
  m = /^(.*)\/[^\/]*$/.match(ds.uri)
  if m
    m[1]
  else
    ::RDF::URI.new(nil)
  end
end

.property(name, *args, &block) ⇒ Object



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

def property(name, *args, &block)
  super
  add_attribute_indexing_config(name, &block) if block_given?
end

.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| parent_uri(ds) }
end

.resource_class(klass = nil) ⇒ Class

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

Parameters:

  • klass (Class) (defaults to: nil)

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

Returns:

  • (Class)

    the object resource class



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 44

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



73
74
75
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 73

def content
  serialize
end

#content=(new_content) ⇒ Object



77
78
79
80
81
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 77

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

#content_changed?Boolean

Returns:

  • (Boolean)


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

def content_changed?
  return false unless instance_variable_defined? :@resource
  return true if empty_or_blank_subject? # can't be serialized because a subject hasn't been assigned yet.
  @content = serialize
  super
end

#deserialize(data = nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 155

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

  # 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

#empty_or_blank_subject?Boolean

Returns:

  • (Boolean)


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

def empty_or_blank_subject?
  resource.rdf_subject.node? || resource.rdf_subject.value.blank?
end

#freezeObject



99
100
101
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 99

def freeze
  @resource.freeze
end

#metadata?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 69

def metadata?
  true
end

#parent_uriObject



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

def parent_uri
  self.class.parent_uri(self)
end

#refresh_attributesObject



129
130
131
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 129

def refresh_attributes
  @resource = nil
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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 109

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)
                  end
                  klass.accepts_nested_attributes_for(*nested_attributes_options.keys) unless nested_attributes_options.blank?
                  uri_stub = self.class.rdf_subject.call(self)

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

#serialization_formatObject



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

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

#serializeObject



150
151
152
153
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 150

def serialize
  resource.set_subject!(parent_uri) if parent_uri 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.



138
139
140
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 138

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

#update_indexed_attributes(hash) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/active_fedora/rdf/rdf_datastream.rb', line 142

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

#uri=(uri) ⇒ Object



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

def uri=(uri)
  super
  resource.set_subject!(parent_uri) if empty_or_blank_subject?
end