Module: TaliaCore::ActiveSourceParts::PredicateHandler::ClassMethods

Included in:
TaliaCore::ActiveSource
Defined in:
lib/talia_core/active_source_parts/predicate_handler.rb

Overview

This file contains the handling of the “predicate wrapper” lists that represent the properties/objects a class has for a given predicate

Instance Method Summary collapse

Instance Method Details

#prefetch_relations_for(sources, limit = 1024) ⇒ Object

Attempts to fetch all relations on the given sources at once, so that there is potentially only one.

For safety reasons, there is a limit on the number of sources that is accepted. (For a web application, if you go over the default, you’re probably doing it wrong).

Raises:

  • (RangeError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 15

def prefetch_relations_for(sources, limit = 1024)
  sources = [ sources ] if(sources.is_a?(ActiveSource))
  raise(RangeError, "Too many sources for prefetching.") if(sources.size > limit)
  src_hash = {}
  sources.each { |src| src_hash[src.id] = src }
  conditions = { :subject_id => src_hash.keys }
  joins = ActiveSource.sources_join
  joins << ActiveSource.props_join
  relations = SemanticRelation.find(:all, :conditions => conditions,
  :joins => joins,
  :select => SemanticRelation.fat_record_select
  )
  relations.each do |rel|
    src_hash[rel.subject_id].inject_predicate(rel)
  end

  # Set all as loaded
  sources.each do |src|
    src.each_cached_wrapper { |wrap| wrap.instance_variable_set(:'@loaded', true) }
    src.instance_variable_set(:'@prefetched', true)
  end
end