Module: TaliaCore::ActiveSourceParts::PredicateHandler
- Included in:
- TaliaCore::ActiveSource
- Defined in:
- lib/talia_core/active_source_parts/predicate_handler.rb
Overview
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.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#each_cached_wrapper ⇒ Object
Loops through the wrapper cache and passes each of the SemanticCollectionWrappers in the cache to the block given to this method.
-
#get_objects_on(predicate) ⇒ Object
Returns the values for the given predicate.
-
#get_wrapper_on(predicate) ⇒ Object
Returns the SemanticCollectionWrapper for the given predicate.
-
#has_type?(type) ⇒ Boolean
Checks if the source has the given RDF type.
-
#inject_predicate(relation) ⇒ Object
Injects a ‘fat relation’ into the cache/wrappter.
-
#reset! ⇒ Object
Resets the internal cache of wrappers/properties.
-
#save_wrappers ⇒ Object
Goes through the existing SemanticCollectionWrappers in the cache, and saves any modifications that may exist.
-
#types ⇒ Object
Gets the RDF types for the source.
Instance Method Details
#each_cached_wrapper ⇒ Object
Loops through the wrapper cache and passes each of the SemanticCollectionWrappers in the cache to the block given to this method
127 128 129 130 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 127 def each_cached_wrapper return unless(@type_cache) @type_cache.each_value { |wrap| yield(wrap) } end |
#get_objects_on(predicate) ⇒ Object
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.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 99 def get_objects_on(predicate) singular = (predicate)[:singular_property].true? wrapper = get_wrapper_on(predicate) if(singular && wrapper.size <= 1) wrapper.first else assit(!singular, 'Was expecting a single value (property is defined as singular). Got more than one.') wrapper end end |
#get_wrapper_on(predicate) ⇒ Object
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)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 75 def get_wrapper_on(predicate) @type_cache ||= {} active_wrapper = @type_cache[predicate.to_s] if(active_wrapper.nil?) active_wrapper = SemanticCollectionWrapper.new(self, predicate) # If this is a prefetched source we have everything, so we can # initialize the wrapper without loading anything active_wrapper.init_as_empty! if(@prefetched) @type_cache[predicate.to_s] = active_wrapper end active_wrapper end |
#has_type?(type) ⇒ Boolean
Checks if the source has the given RDF type
61 62 63 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 61 def has_type?(type) (self.types.include?(type)) end |
#inject_predicate(relation) ⇒ Object
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.
142 143 144 145 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 142 def inject_predicate(relation) wrapper = get_wrapper_on(relation.predicate_uri) wrapper.insert_item(relation) end |
#reset! ⇒ Object
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
135 136 137 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 135 def reset! @type_cache = nil end |
#save_wrappers ⇒ Object
Goes through the existing SemanticCollectionWrappers in the cache, and saves any modifications that may exist.
This is automatically called when the source is saved.
115 116 117 118 119 120 121 122 123 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 115 def save_wrappers each_cached_wrapper do |wrap| # Load unloaded if we're not rdf_autosaving. Quick hack since otherwise # since the blanking of unloaded properties could cause problems with # the rdf writing otherwise wrap.send(:load!) unless(wrap.loaded? || autosave_rdf?) wrap.save_items! end end |
#types ⇒ Object
Gets the RDF types for the source. This is equivalent to accessing the rdf:type predicate.
56 57 58 |
# File 'lib/talia_core/active_source_parts/predicate_handler.rb', line 56 def types get_objects_on(N::RDF.type.to_s) end |