Sha256: 2031db6c3e17334f363196c92d9935265646ee4c2effacdc08bc131b9fb7b3b9

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

module ActiveFedora
  # a Hash of properties that have changed and their present values
  class ChangeSet

    attr_reader :object, :graph, :changed_attributes

    # @param [ActiveFedora::Base] object The resource that has associations and properties
    # @param [RDF::Graph] graph The RDF graph that holds the current state
    # @param [Array] changed_attributes A list of properties that have changed
    def initialize(object, graph, changed_attributes)
      @object = object
      @graph = graph
      @changed_attributes = changed_attributes
    end

    def empty?
      changes.empty?
    end

    def changes
      @changes ||= changed_attributes.each_with_object({}) do |key, result|
        if object.respond_to?(:association) && object.association(key.to_sym).present?
          predicate = object.association(key.to_sym).reflection.predicate
          result[predicate] = graph.query(predicate: predicate)
        elsif object.class.properties.keys.include?(key)
          predicate = graph.reflections.reflect_on_property(key).predicate
          result[predicate] = graph.query(predicate: predicate)
        elsif object.local_attributes.include?(key)
          raise "Unable to find a graph predicate corresponding to the attribute: \"#{key}\""
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active-fedora-9.0.8 lib/active_fedora/change_set.rb
active-fedora-9.1.1 lib/active_fedora/change_set.rb
active-fedora-9.1.0 lib/active_fedora/change_set.rb
active-fedora-9.1.0.rc1 lib/active_fedora/change_set.rb
active-fedora-9.0.6 lib/active_fedora/change_set.rb
active-fedora-9.0.5 lib/active_fedora/change_set.rb