Module: Krikri::LDP::Invalidatable

Included in:
RdfSource, OriginalRecord
Defined in:
lib/krikri/ldp/invalidatable.rb

Overview

Implements invalidation for ‘Krikri::LDP::Resource`s. This is different from deletion, in that the resource continues to respond `200 OK`, and return the representation, Nothing is removed from the LDP server.

Works as a mixin to ‘Krikri::LDP::Resource`, assuming an implementation of `#rdf_source`, which may simply return `self`.

Examples:

invalidating a resource

class MyResource
  include Krikri::LDP::Resource
  include Krikri::LDP::Invalidatable

  def rdf_subject
    @rdf_subject ||= RDF::URI('http://example.com/ldp/a/resource/path')
  end
end

invalidatable_resource = MyResource.new
# the resource must exist before it can be invalidated! 
invalidatable_resource.save

invalidatable_resource.invalidate!
invalidatable_resource.invalidated? # => true
invalidatable_resource.invalidated_at_time 
# => Thu, 03 Dec 2015 10:27:45 -0800

See Also:

Constant Summary collapse

INVALIDATED_BY_URI =

See Also:

  • RDF::PROV
RDF::PROV.wasInvalidatedBy
INVALIDATED_TIME_URI =
RDF::PROV.invalidatedAtTime

Instance Method Summary collapse

Instance Method Details

#invalidate!(activity_uri = nil, ignore_invalid = false) ⇒ void

This method returns an undefined value.

Invalidates the resource by marking it with a ‘prov:invalidatedAtTime`. If an `RDF::Term` is passed as the first argument, that term is used as the value of `prov:wasInvalidatedBy`.

Examples:

invalidating with an activity

invalidatable_resource.invalidate!(RDF::URI('http://example.org/moomin'))
invalidatable_resource.was_invalidated_by
# => #<RDF::URI:0x2acab846109c URI:http://example.org/moomin>

Parameters:

  • activity_uri (RDF::Term) (defaults to: nil)

    a URI for the invalidating activity. If none is given, this defaults to ‘nil` and no `prov:wasInvalidatedBy` statement is added.

  • ignore_invalid (Boolean) (defaults to: false)

    if true, supresses errors on already, invalid records

Raises:

  • (RuntimeError)

    when the resource does not exist or is already invalid; unless ‘ignore_invalid` is `true`



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/krikri/ldp/invalidatable.rb', line 55

def invalidate!(activity_uri = nil, ignore_invalid = false)
  raise "Cannot invalidate #{rdf_subject}, does not exist." unless exists?

  # force a reload unless we have cached an invalidatedAtTime
  rdf_source.get({}, true) unless invalidated?
  # we check invalidated again in case the reload came back invalid
  if invalidated?
    return if ignore_invalid
    raise "Cannot invalidate #{rdf_subject}, already invalid." 
  end

  uri = RDF::URI(rdf_subject)
  
  rdf_source << [uri, INVALIDATED_BY_URI, activity_uri] unless 
    activity_uri.nil?
  rdf_source << [uri, INVALIDATED_TIME_URI, DateTime.now]

  rdf_source.save
end

#invalidated?Boolean

Returns ‘true` if the resource has been marked invalidated.

Returns:

  • (Boolean)

    ‘true` if the resource has been marked invalidated.



77
78
79
# File 'lib/krikri/ldp/invalidatable.rb', line 77

def invalidated?
  !invalidated_at_time.nil?
end

#invalidated_at_timeDateTime?

Note:

if two invalidatedAtTimes exist, we may get either of them back!

Returns the time this resource was marked invalidated; gives ‘nil` if the resource has not been invalidated.

Returns:

  • (DateTime, nil)

    the time this resource was marked invalidated; gives ‘nil` if the resource has not been invalidated.



86
87
88
89
# File 'lib/krikri/ldp/invalidatable.rb', line 86

def invalidated_at_time
  time = first_property(INVALIDATED_TIME_URI)
  time.nil? ? nil : time.object
end

#was_invalidated_byRDF::URI?

Note:

if two wasInvalidatedBys exist, we may get either of them back!

Returns the activity responsible for invalidating the resource.

Returns:

  • (RDF::URI, nil)

    the activity responsible for invalidating the resource



96
97
98
# File 'lib/krikri/ldp/invalidatable.rb', line 96

def was_invalidated_by
  first_property(INVALIDATED_BY_URI)
end