| Module | TaliaCore::ActiveSourceParts::PredicateHandler |
| In: |
lib/talia_core/active_source_parts/predicate_handler.rb
|
Methods for ActiveSource objects for accessing and handling predicates, which are the properties connected to the source. When accessing a predicate/property of an ActiveSource, the system will return a SemanticCollectionWrapper.
Once a predicate is loaded, the ActiveSource will cache the SemanticCollectionWrapper internally, and will re-use it on subsequent accesses.
If the relations are prefetched (usually by providing the :prefetch_relations option to the find Method, see the Finders module), all wrappers for the source are loaded at once; the actual prefetching is done by the prefetch_relations_for method, which can also be used directly on a collection of sources.
Loops through the wrapper cache and passes each of the SemanticCollectionWrappers in the cache to the block given to this method
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 127
127: def each_cached_wrapper
128: return unless(@type_cache)
129: @type_cache.each_value { |wrap| yield(wrap) }
130: end
Returns the values for the given predicate. This will work like get_wrapper_on except if the predicate is declared as a :singular_property with ActiveSouce.property_options (or singular_property, or multi_property). In that case, it will return only a single value. However, if the predicate is declare singular and there already is more than one value, it will return the wrapper (and fail an assit). In general, a property that was defined singular and contains more than one value indicates a problem with the application.
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 99
99: def get_objects_on(predicate)
100: singular = property_options_for(predicate)[:singular_property].true?
101: wrapper = get_wrapper_on(predicate)
102:
103: if(singular && wrapper.size <= 1)
104: wrapper.first
105: else
106: assit(!singular, 'Was expecting a single value (property is defined as singular). Got more than one.')
107: wrapper
108: end
109: end
Returns the SemanticCollectionWrapper for the given predicate. The collection wrapper will be cached internally, so that subsequent calls will receive the same collection wrapper again.
This also means that any modifications to the wrapper are preserved in the cache - if a wrapper is modified in memory, and accessed again _on the same source_, a subsequent access will return the modified wrapper.
Modified wrappers are saved when the ActiveSource itself is saved (through save_wrappers, which is automatically called)
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 75
75: def get_wrapper_on(predicate)
76: @type_cache ||= {}
77: active_wrapper = @type_cache[predicate.to_s]
78:
79: if(active_wrapper.nil?)
80: active_wrapper = SemanticCollectionWrapper.new(self, predicate)
81:
82: # If this is a prefetched source we have everything, so we can
83: # initialize the wrapper without loading anything
84: active_wrapper.init_as_empty! if(@prefetched)
85:
86: @type_cache[predicate.to_s] = active_wrapper
87: end
88:
89: active_wrapper
90: end
Checks if the source has the given RDF type
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 61
61: def has_type?(type)
62: (self.types.include?(type))
63: end
Injects a ‘fat relation’ into the cache/wrappter. A "fat" relation is a SemanticRelation which contains additional fields (e.g. the subject uri, all object information, etc.) - See also the SemanticCollectionWrapper documentation.
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 142
142: def inject_predicate(relation)
143: wrapper = get_wrapper_on(relation.predicate_uri)
144: wrapper.insert_item(relation)
145: end
Resets the internal cache of wrappers/properties. Any unsaved changes on the wrappers are lost, and get_object_on will have to reload all data when it is called again
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 135
135: def reset!
136: @type_cache = nil
137: end
Goes through the existing SemanticCollectionWrappers in the cache, and saves any modifications that may exist.
This is automatically called when the source is saved.
# File lib/talia_core/active_source_parts/predicate_handler.rb, line 115
115: def save_wrappers
116: each_cached_wrapper do |wrap|
117: # Load unloaded if we're not rdf_autosaving. Quick hack since otherwise
118: # since the blanking of unloaded properties could cause problems with
119: # the rdf writing otherwise
120: wrap.send(:load!) unless(wrap.loaded? || autosave_rdf?)
121: wrap.save_items!
122: end
123: end