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.

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:



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

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

#by_subject(fact_subject) ⇒ Object



46
47
48
# File 'lib/dbd/fact/collection.rb', line 46

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

#initializeObject



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

def initialize
  super
  @hash_by_subject = Hash.new { |h, k| h[k] = [] }
end

#newest_time_stampObject



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

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

#oldest_time_stampObject



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

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

#subjectsObject



50
51
52
# File 'lib/dbd/fact/collection.rb', line 50

def subjects
  @hash_by_subject.keys
end