Module: Dbd::Fact::Collection

Includes:
Helpers::OrderedSetCollection
Included in:
Graph
Defined in:
lib/dbd/fact/collection.rb

Instance Method Summary collapse

Methods included from Helpers::OrderedSetCollection

add_and_return_index, #each, #freeze, #last, #size

Instance Method Details

#<<(fact) ⇒ self

This is the central method of Fact::Collection module

Validates that added fact is valid (has no errors).

Validates that added fact is newer.

Adds the fact and return the index in the collection.

Store this index in the hash_by_subject.

Parameters:

  • fact (Fact)

    the fact that is added to the collection

Returns:

  • (self)

    for chaining

Raises:



39
40
41
42
43
44
# File 'lib/dbd/fact/collection.rb', line 39

def <<(fact)
  raise FactError, "#{fact.errors.join(', ')}." unless fact.errors.empty?
  validate_time_stamp(fact)
  add_to_collection(fact)
  self
end

#by_subject(fact_subject) ⇒ Object

This method works on resource and context subjects.

Should be less relevant, now that resources and contexts are available as methods to get proper access.



51
52
53
54
55
# File 'lib/dbd/fact/collection.rb', line 51

def by_subject(fact_subject)
  hash_entry_from_indices(fact_subject).map do |index|
    @internal_collection[index]
  end
end

#context_subjectsObject

Use this to get all contexte subjects. But if the contexts themselves are needed, there is a direct Graph#contexts method.



67
68
69
# File 'lib/dbd/fact/collection.rb', line 67

def context_subjects
  @context_indices_by_subject.keys
end

#initializeObject



9
10
11
12
13
# File 'lib/dbd/fact/collection.rb', line 9

def initialize
  super
  @resource_indices_by_subject = {}
  @context_indices_by_subject = {}
end

#newest_time_stampObject



15
16
17
18
# File 'lib/dbd/fact/collection.rb', line 15

def newest_time_stamp
  newest_entry = @internal_collection.last
  newest_entry && newest_entry.time_stamp
end

#oldest_time_stampObject



20
21
22
23
# File 'lib/dbd/fact/collection.rb', line 20

def oldest_time_stamp
  oldest_entry = @internal_collection.first
  oldest_entry && oldest_entry.time_stamp
end

#resource_subjectsObject

Use this to get all resource subjects. But if the resources themselves are needed, there is a direct Graph#resources method.



60
61
62
# File 'lib/dbd/fact/collection.rb', line 60

def resource_subjects
  @resource_indices_by_subject.keys
end