Class: ActiveFedora::ChangeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/change_set.rb

Overview

a Hash of properties that have changed and their present values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, graph, changed_attributes) ⇒ ChangeSet

Returns a new instance of ChangeSet.

Parameters:

  • object (ActiveFedora::Base)

    The resource that has associations and properties

  • graph (RDF::Graph)

    The RDF graph that holds the current state

  • changed_attributes (Array)

    A list of properties that have changed



9
10
11
12
13
# File 'lib/active_fedora/change_set.rb', line 9

def initialize(object, graph, changed_attributes)
  @object = object
  @graph = graph
  @changed_attributes = changed_attributes
end

Instance Attribute Details

#changed_attributesObject (readonly)

Returns the value of attribute changed_attributes.



4
5
6
# File 'lib/active_fedora/change_set.rb', line 4

def changed_attributes
  @changed_attributes
end

#graphObject (readonly)

Returns the value of attribute graph.



4
5
6
# File 'lib/active_fedora/change_set.rb', line 4

def graph
  @graph
end

#objectObject (readonly)

Returns the value of attribute object.



4
5
6
# File 'lib/active_fedora/change_set.rb', line 4

def object
  @object
end

Instance Method Details

#changesHash<RDF::URI, RDF::Queryable::Enumerator>

Returns hash of predicate uris to statements.

Returns:

  • (Hash<RDF::URI, RDF::Queryable::Enumerator>)

    hash of predicate uris to statements



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_fedora/change_set.rb', line 18

def changes
  @changes ||= changed_attributes.each_with_object({}) do |key, result|
    if object.association(key.to_sym).present?
      # This is always an ActiveFedora::Reflection::RDFPropertyReflection
      predicate = object.association(key.to_sym).reflection.predicate
      result[predicate] = graph.query({ subject: object.rdf_subject, predicate: predicate })
    elsif object.class.properties.keys.include?(key)
      predicate = graph.reflections.reflect_on_property(key).predicate
      results = graph.query({ subject: object.rdf_subject, predicate: predicate })
      new_graph = child_graphs(results.map(&:object))
      results.each do |res|
        new_graph << res
      end
      result[predicate] = new_graph
    elsif key == 'type'.freeze
      # working around https://github.com/ActiveTriples/ActiveTriples/issues/122
      predicate = ::RDF.type
      result[predicate] = graph.query({ subject: object.rdf_subject, predicate: predicate }).select do |statement|
        !statement.object.to_s.start_with?("http://fedora.info/definitions/v4/repository#", "http://www.w3.org/ns/ldp#")
      end
    elsif object.local_attributes.include?(key)
      raise "Unable to find a graph predicate corresponding to the attribute: \"#{key}\""
    end
  end
end