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, #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.

Validates that subject was never used as provenance_subject [A].

Adds the fact and return the index in the collection.

Store this index in the hash_by_subject.

Mark the fact in the list of used provenance_subjects (for [A]).

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/dbd/fact/collection.rb', line 43

def <<(fact)
  raise FactError, "#{fact.errors.join(', ')}." unless fact.errors.empty?
  raise OutOfOrderError if (self.newest_time_stamp && fact.time_stamp <= self.newest_time_stamp)
  raise OutOfOrderError if (@used_provenance_subjects[fact.subject])
  index = Helpers::OrderedSetCollection.add_and_return_index(fact, @internal_collection)
  @hash_by_subject[fact.subject] << index
  fact.update_used_provenance_subjects(@used_provenance_subjects)
  self
end

#by_subject(fact_subject) ⇒ Object



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

def by_subject(fact_subject)
  @hash_by_subject[fact_subject].map{ |index| @internal_collection[index]}
end

#initializeObject



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

def initialize
  super
  @hash_by_subject = Hash.new { |h, k| h[k] = [] }
  @used_provenance_subjects = {}
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