Class: TaliaCore::SemanticCollectionWrapper
- Inherits:
-
Object
- Object
- TaliaCore::SemanticCollectionWrapper
- Includes:
- Enumerable
- Defined in:
- lib/talia_core/semantic_collection_wrapper.rb
Overview
Wraps the Array/Collection returned from the ActiveRecord, simply “hiding” the SemanticProperty objects behind strings.
Instance Attribute Summary collapse
-
#force_type ⇒ Object
readonly
Returns the value of attribute force_type.
Class Method Summary collapse
-
.special_types ⇒ Object
Simple hash that checks if a type if property requires “special” handling This will cause the wrapper to accept ActiveSource relations and all sources will be casted to the given type.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
(also: #concat)
Push to collection.
-
#add_with_order(value, order) ⇒ Object
Adds the object and gives the relation the given order.
-
#at(index) ⇒ Object
(also: #[])
Get the element ”‘value”’ at the given index.
-
#clean? ⇒ Boolean
Indicates that the wraper is “clean”, that is it hasn’t been written to or read from.
-
#collect ⇒ Object
Collect method for the semantic wrapper.
-
#each ⇒ Object
Iterates over each ”‘value”’ of the items in the relation.
-
#each_item ⇒ Object
Iterates of each ”‘target”’ of the items in the relation.
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#get_item_at(index) ⇒ Object
Gets the value at the given index.
-
#include?(value) ⇒ Boolean
Check if the collection includes the value.
-
#index(value) ⇒ Object
Index of the given value.
-
#init_as_empty! ⇒ Object
Forces this relation to be empty.
-
#initialize(source, predicate) ⇒ SemanticCollectionWrapper
constructor
Initialize the list.
-
#inject_fat_item(fat_rel) ⇒ Object
Injector for a fat relation.
-
#join(join_str = ', ') ⇒ Object
Joins the elments into a string.
- #last ⇒ Object
-
#loaded? ⇒ Boolean
Indicates of the internal collection is loaded.
-
#remove(*params) ⇒ Object
Remove the given value.
-
#replace(old_value, new_value) ⇒ Object
Replace a value with a new one.
-
#save_items! ⇒ Object
This attempts to save the items to the database.
-
#size ⇒ Object
Size of the collection.
-
#values ⇒ Object
Returns an array with all values in the collection.
-
#values_with_lang(language = 'en') ⇒ Object
Returns only the values of the given language.
Constructor Details
#initialize(source, predicate) ⇒ SemanticCollectionWrapper
Initialize the list
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 21 def initialize(source, predicate) # raise(ActiveRecord::RecordNotSaved, "No properties on unsaved record.") if(source.new_record?) @assoc_source = source @assoc_predicate = if(predicate.respond_to?(:uri)) predicate.uri.to_s else predicate.to_s end @force_type = self.class.special_types[@assoc_predicate] end |
Instance Attribute Details
#force_type ⇒ Object (readonly)
Returns the value of attribute force_type.
9 10 11 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 9 def force_type @force_type end |
Class Method Details
.special_types ⇒ Object
Simple hash that checks if a type if property requires “special” handling This will cause the wrapper to accept ActiveSource relations and all sources will be casted to the given type
14 15 16 17 18 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 14 def self.special_types @special_types ||= { N::RDF.type.to_s => N::SourceClass } end |
Instance Method Details
#<<(value) ⇒ Object Also known as: concat
Push to collection. Giving a string will create a property to be created, saved and associated.
136 137 138 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 136 def <<(value) add_with_order(value, nil) end |
#add_with_order(value, order) ⇒ Object
Adds the object and gives the relation the given order.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 142 def add_with_order(value, order) # We use order exclusively for "ordering" predicates assit_equal(TaliaCore::OrderedSource.index_to_predicate(order), @assoc_predicate) if(order) raise(ArgumentError, "cannot add nil") unless(value != nil) if(value.kind_of?(Array)) value.each { |v| add_record_for(v, order) } else add_record_for(value, order) end end |
#at(index) ⇒ Object Also known as: []
Get the element ”‘value”’ at the given index
33 34 35 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 33 def at(index) items.at(index).value if(items.at(index)) end |
#clean? ⇒ Boolean
Indicates that the wraper is “clean”, that is it hasn’t been written to or read from
209 210 211 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 209 def clean? @items.nil? end |
#collect ⇒ Object
Collect method for the semantic wrapper
59 60 61 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 59 def collect items.collect { |item| yield(item.value) } end |
#each ⇒ Object
Iterates over each ”‘value”’ of the items in the relation.
54 55 56 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 54 def each items.each { |item| yield(item.value) } end |
#each_item ⇒ Object
Iterates of each ”‘target”’ of the items in the relation. (This will pass in SemanticProperty objects instead of the value
65 66 67 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 65 def each_item items.each { |item| yield(item.object) } end |
#empty? ⇒ Boolean
213 214 215 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 213 def empty? self.size == 0 end |
#first ⇒ Object
38 39 40 41 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 38 def first item = items.first item ? item.value : nil end |
#get_item_at(index) ⇒ Object
Gets the value at the given index
49 50 51 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 49 def get_item_at(index) items.at(index).object if(items.at(index)) end |
#include?(value) ⇒ Boolean
Check if the collection includes the value
128 129 130 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 128 def include?(value) items.include?(value) end |
#index(value) ⇒ Object
Index of the given value
123 124 125 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 123 def index(value) items.index(value) end |
#init_as_empty! ⇒ Object
Forces this relation to be empty. This initializes the relation as if no elements exist. This doesn’t look anything up in the databse. Warning Only call this if you need an empty wrapper that doesn’t look up anything in the database
229 230 231 232 233 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 229 def init_as_empty! raise(ArgumentError, "Already initialized!") if(loaded?) @items = [] @loaded = true end |
#inject_fat_item(fat_rel) ⇒ Object
Injector for a fat relation. This must take place before flagging the source as “loaded”
219 220 221 222 223 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 219 def inject_fat_item(fat_rel) raise(RuntimeError, 'Trying to inject in loaded object.') if(loaded?) @items ||= [] @items << SemanticCollectionItem.new(fat_rel, :fat) end |
#join(join_str = ', ') ⇒ Object
Joins the elments into a string
117 118 119 120 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 117 def join(join_str = ', ') strs = items.collect { |item| item.value.to_s } strs.join(join_str) end |
#last ⇒ Object
43 44 45 46 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 43 def last item = items.last item ? item.value : nil end |
#loaded? ⇒ Boolean
Indicates of the internal collection is loaded
203 204 205 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 203 def loaded? @loaded end |
#remove(*params) ⇒ Object
Remove the given value. With no parameters, the whole list will be cleared and the RDF will be updated immediately.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 162 def remove(*params) if(params.length > 0) params.each { |par| remove_relation(par) } else if(loaded?) items.each { |item| item.relation.destroy } else SemanticRelation.destroy_all( :subject_id => @assoc_source.id, :predicate_uri => @assoc_predicate ) end @assoc_source.my_rdf.remove(N::URI.new(@assoc_predicate)) @items = [] @loaded = true end end |
#replace(old_value, new_value) ⇒ Object
Replace a value with a new one
154 155 156 157 158 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 154 def replace(old_value, new_value) idx = items.index(old_value) items[idx].destroy add_record_for(new_value) { |new_item| items[idx] = new_item } end |
#save_items! ⇒ Object
This attempts to save the items to the database
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 181 def save_items! return if(clean?) # If there are no items, nothing was modified @assoc_source.save! unless(@assoc_source.id) @items.each do |item| next if(item.fat_relation) # we skip the fat relations, they are never new and never saveable rel = item.plain_relation must_save = rel.new_record? if(rel.object_id.nil?) rel.object.save! if(rel.object.new_record?) rel.object_id = rel.object.id must_save = true end unless(rel.subject_id != nil) rel.subject_id = @assoc_source.id must_save = true end rel.save! if(must_save) end @items = nil unless(loaded?) # Otherwise we'll have trouble reload-and merging end |
#size ⇒ Object
Size of the collection.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 103 def size return items.size if(loaded?) if(@items) # This is not really possible without loading, so we do it load! items.size else SemanticRelation.count(:conditions => { 'subject_id' => @assoc_source.id, 'predicate_uri' => @assoc_predicate }) end end |
#values ⇒ Object
Returns an array with all values in the collection
70 71 72 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 70 def values items.collect { |item| item.value } end |
#values_with_lang(language = 'en') ⇒ Object
Returns only the values of the given language. (At the moment this is not aware of region codes or any specialities, it just does a string matching)
If no values with the given locale are found, this will fall back on the default locale and then to the values that don’t have a locale at all.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/talia_core/semantic_collection_wrapper.rb', line 81 def values_with_lang(language = 'en') language_is_default = (language == I18n.default_locale.to_s) real = [] default = [] unset = [] items.each do |item| # FIXME: At the moment, this only works for value attributes, not for # sources if((val = item.value).respond_to?(:lang)) real << val if(val.lang == language) default << val if(!language_is_default && (val.lang == I18n.default_locale.to_s)) unset << val if(val.lang.blank?) else default << val end end return real unless(real.empty?) return default unless(default.empty?) unset end |