module Dbd::Fact::Collection
Public Class Methods
Dbd::Helpers::OrderedSetCollection::new
# File lib/dbd/fact/collection.rb, line 9 def initialize super @resource_indices_by_subject = {} @context_indices_by_subject = {} end
Public Instance Methods
This is the central method of Fact::Collection
module
@param [Fact] fact the fact that is added to the collection
@return [self] for chaining
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.
# 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
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.
# 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
Use this to get all contexte subjects. But if the contexts themselves are needed, there is a direct Graph#contexts
method.
# File lib/dbd/fact/collection.rb, line 67 def context_subjects @context_indices_by_subject.keys end
# File lib/dbd/fact/collection.rb, line 15 def newest_time_stamp newest_entry = @internal_collection.last newest_entry && newest_entry.time_stamp end
# File lib/dbd/fact/collection.rb, line 20 def oldest_time_stamp oldest_entry = @internal_collection.first oldest_entry && oldest_entry.time_stamp end
Use this to get all resource subjects. But if the resources themselves are needed, there is a direct Graph#resources
method.
# File lib/dbd/fact/collection.rb, line 60 def resource_subjects @resource_indices_by_subject.keys end
Private Instance Methods
# File lib/dbd/fact/collection.rb, line 87 def add_to_collection(fact) index = Helpers::OrderedSetCollection.add_and_return_index(fact, @internal_collection) add_to_index_hash(fact, index) end
# File lib/dbd/fact/collection.rb, line 92 def add_to_index_hash(fact, index) if fact.context_fact? add_to_index_hash_with_default_array(@context_indices_by_subject, fact.subject, index) else add_to_index_hash_with_default_array(@resource_indices_by_subject, fact.subject, index) end end
# File lib/dbd/fact/collection.rb, line 100 def add_to_index_hash_with_default_array(index_hash, subject, index) if (array = index_hash[subject]) array << index else index_hash[subject] = [index] end end
Because the subject are either in the resource or the context index, we need to search both. A subject should never be in both, but we do currently not defend against that error.
# File lib/dbd/fact/collection.rb, line 77 def hash_entry_from_indices(fact_subject) @resource_indices_by_subject[fact_subject] || @context_indices_by_subject[fact_subject] end
# File lib/dbd/fact/collection.rb, line 81 def validate_time_stamp(fact) if (newest_time_stamp && fact.time_stamp <= newest_time_stamp) raise OutOfOrderError, "time_stamp of fact was too old : #{fact.time_stamp}" end end