Class: Dbd::Resource

Inherits:
Object
  • Object
show all
Includes:
Helpers::OrderedSetCollection
Defined in:
lib/dbd/resource.rb

Overview

A Resource is a collection of facts that have the same subject.

In the real-world this is a mainly an "instance" about which all facts are giving information (e.g. a conference, a person, a bicycle, ...). More generally this can also be used to describe classes and other concepts in the software system.

A new (random) subject is generated for a resource. In Dbd, a subject is a random uuid (like a oid), not a meaningful URI as it is in RDF.

A provenance_subject is a required field in the options hash. Practically, first a ProvenanceResource will be created and the subject of that will be used as provenance_subject for the Resources that are associated with it.

During build-up of a Fact, the subject and the provenance_subject can be nil. These will then be set when the Fact is added (with '<<') to a resource.

Direct Known Subclasses

ProvenanceResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::OrderedSetCollection

add_and_return_index, #each, #last, #size

Constructor Details

#initialize(options) ⇒ Resource

Build a new resource.

By default, a new (random) subject is generated for a resource. Optionally, an explicit subject can be given in the options parameter (this is best created with the new_subject class method for forward compatibility).

The provenance_subject argument is required. This will typically be taken from an earlier created ProvenanceResource.

Parameters:

  • options (Hash{Symbol => Object})

Options Hash (options):

  • :provenance_subject (Fact::Subject) — default: required

    the subject of the provenance resource for this resource

  • :subject (Fact::Subject) — default: new_subject

    Optional: the subject for the resource



57
58
59
60
61
62
# File 'lib/dbd/resource.rb', line 57

def initialize(options)
  @subject = options[:subject] || self.class.new_subject
  @provenance_subject = options[:provenance_subject]
  validate_provenance_subject
  super()
end

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



28
29
30
# File 'lib/dbd/resource.rb', line 28

def subject
  @subject
end

Class Method Details

.new_subjectFact::Subject

Returns a new (random) Resource subject.

Returns:



40
41
42
# File 'lib/dbd/resource.rb', line 40

def self.new_subject
  Fact.new_subject
end

Instance Method Details

#<<(fact) ⇒ Object

Add a fact.

  • if it has no subject, the subject is set (this modifies the fact !)
  • if is has the same subject as the resource, added unchanged.
  • if it has a different subject, a SubjectError is raised.
  • if it has no provenance_subject, the provenance_subject is set (this modifies the fact !)
  • if is has the same provenance_subject as the resource, added unchanged.
  • if it has a different provenance_subject, a ProvenanceError is raised.


73
74
75
76
77
78
# File 'lib/dbd/resource.rb', line 73

def <<(fact)
  # TODO: check the type of the fact (Fact)
  set_subject!(fact)
  set_provenance!(fact)
  super(fact)
end

#provenance_subjectObject

Getter for provenance_subject.

Will be overridden in the ProvenanceResource subclass.



34
35
36
# File 'lib/dbd/resource.rb', line 34

def provenance_subject
  @provenance_subject
end