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 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 context_subject can optionally be given in the options hash. The context_subject of the Resource will be used as a default for Facts that are added to the Resource.

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

Direct Known Subclasses

Context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::OrderedSetCollection

add_and_return_index, #each, #freeze, #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 context_subject argument is optional (if all facts in the resource have the same context).

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    (optional)

Options Hash (options):

  • :context_subject (Fact::Subject) — default: nil

    Optional: the subject of the context for this resource

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

    Optional: the subject for the resource



49
50
51
52
53
# File 'lib/dbd/resource.rb', line 49

def initialize(options = {})
  set_subject(options)
  set_context_subject(options)
  super()
end

Instance Attribute Details

#context_subjectObject (readonly)

Returns the value of attribute context_subject.



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

def context_subject
  @context_subject
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Class Method Details

.new_subjectFact::Subject

Returns a new (random) Resource subject.

Returns:



31
32
33
# File 'lib/dbd/resource.rb', line 31

def self.new_subject
  Fact.factory.new_subject
end

Instance Method Details

#<<(fact_collection) ⇒ Resource

Add a Fact (strictly not a ContextFact) or recursive collection of facts

Side effects on subject and context_subject:

  • 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.

  • inside one resource, all facts must have same subject

  • if it has no context_subject, the context_subject is set (this modifies the fact !)

  • if is has a context_subject this remains unchanged

  • inside one resource, different facts can have different context_subjects

Parameters:

  • fact_collection (Fact, #each)

    a recursive collection of Facts

Returns:



70
71
72
73
74
75
76
# File 'lib/dbd/resource.rb', line 70

def <<(fact_collection)
  fact_collection.each_recursively do |fact|
    prepare_fact!(fact)
    super(fact)
  end
  self
end