Class: TaliaCore::OrderedSource
- Inherits:
-
ActiveSource
- Object
- ActiveRecord::Base
- ActiveSource
- TaliaCore::OrderedSource
- Defined in:
- lib/talia_core/ordered_source.rb
Overview
This class provides an ordering on the related sources. It will load the related elements into an array. All changes made to the OrderedSource will reflect on the Array, and will only be persisted to the store on save.
The collection is contained in the ordered_objects - if you ever use that accessor all “ordering” relations for the object will be completely overwritten on saving. (You can still assign the predicates manually as long as the ordered_objects accesssor is not called before saving.)
Instance Attribute Summary collapse
-
#current_index ⇒ Object
readonly
Returns the value of attribute current_index.
Class Method Summary collapse
-
.index_to_predicate(index) ⇒ Object
return string for index.
-
.new(uri) ⇒ Object
Initialize SeqContainer.
-
.predicate_to_index(predicate) ⇒ Object
return index of predicate.
Instance Method Summary collapse
-
#add(object) ⇒ Object
Add new item to ordered source.
-
#at(index) ⇒ Object
return the item at position index.
-
#delete(index) ⇒ Object
remove an existing object to ordered source.
-
#delete_all ⇒ Object
remove all existing object to ordered source TODO: This will not reliably delete all elements, since the size value is not reliable.
-
#elements ⇒ Object
Returns all elements (not the relations) in an ordered array.
-
#find_position_by_object(object) ⇒ Object
Find the position index of the given object.
-
#first ⇒ Object
Returns the first element of the collection.
-
#index_to_predicate(index) ⇒ Object
return string for index.
-
#insert_at(index, object) ⇒ Object
Inserts an element at the given index.
-
#next(current_element = nil) ⇒ Object
return next item * current_element: int or string.
-
#ordered_objects ⇒ Object
Returns all the objects that are ordered in an array where the array index equals the position of the object in the ordered set.
-
#predicate_to_index(predicate) ⇒ Object
return index of predicate.
-
#previous(current_element = nil) ⇒ Object
return previous item * current_element: int or string.
-
#replace(index, object) ⇒ Object
replace item as position index with object * index: int * object: TaliaCore::ActiveSource.
-
#size ⇒ Object
Return the “size” of the collection.
Methods inherited from ActiveSource
#[], #[]=, #add_additional_rdf_types, #add_semantic_attributes, #attach_files, #data, #db_attr?, #direct_predicates, #inverse, #inverse_predicates, #predicate, #predicate_replace, #predicate_set, #predicate_set_uniq, #rdf_selftype, #rewrite_attributes, #rewrite_attributes!, #short_uri, #to_rdf, #to_s, #to_uri, #to_xml, #update_attributes, #update_attributes!, #update_attributes_orig, #update_attributes_orig!, #update_source, #value_for, #write_predicate_direct
Methods included from ActiveSourceParts::ClassMethods
#additional_rdf_types, #create_from_xml, #create_multi_from, #create_source, #db_attr?, #exists?, #expand_uri, #new, #paginate, #rewrite, #split_attribute_hash, #update, #value_for
Methods included from ActiveSourceParts::Finders
#count, #find, #find_by_partial_local, #find_by_partial_uri, #find_by_uri_token
Methods included from ActiveSourceParts::SqlHelper
#default_inv_joins, #default_joins, #props_join, #sources_join
Methods included from ActiveSourceParts::PredicateHandler::ClassMethods
Methods included from TaliaUtil::Progressable
#progressor, #progressor=, #run_with_progress
Methods included from ActiveSourceParts::Rdf
#autosave_rdf=, #autosave_rdf?, #create_rdf, #my_rdf, #to_rdf
Methods included from ActiveSourceParts::PredicateHandler
#each_cached_wrapper, #get_objects_on, #has_type?, #inject_predicate, #reset!, #save_wrappers, #types
Instance Attribute Details
#current_index ⇒ Object (readonly)
Returns the value of attribute current_index.
13 14 15 |
# File 'lib/talia_core/ordered_source.rb', line 13 def current_index @current_index end |
Class Method Details
.index_to_predicate(index) ⇒ Object
return string for index
162 163 164 |
# File 'lib/talia_core/ordered_source.rb', line 162 def self.index_to_predicate(index) 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_' << ("%06d" % index.to_i) end |
.new(uri) ⇒ Object
Initialize SeqContainer
18 19 20 21 22 23 |
# File 'lib/talia_core/ordered_source.rb', line 18 def self.new(uri) @resource = super(uri) # @resource.save! # @resource.types << RDF::Seq.uri # @resource end |
.predicate_to_index(predicate) ⇒ Object
return index of predicate
167 168 169 |
# File 'lib/talia_core/ordered_source.rb', line 167 def self.predicate_to_index(predicate) predicate.sub('http://www.w3.org/1999/02/22-rdf-syntax-ns#_', '').to_i end |
Instance Method Details
#add(object) ⇒ Object
Add new item to ordered source. This will add the object after the last element. ATTENTION: If you add on an empty collection, it will start at index 1 (One), for backwards compatability
99 100 101 102 |
# File 'lib/talia_core/ordered_source.rb', line 99 def add(object) position = (size > 0) ? size : 1 insert_at(position, object) end |
#at(index) ⇒ Object
return the item at position index.
* index: int
* return value: TaliaCore::ActiveSource
44 45 46 47 |
# File 'lib/talia_core/ordered_source.rb', line 44 def at(index) @current_index = index ordered_objects.at(index) end |
#delete(index) ⇒ Object
remove an existing object to ordered source. This will reorder the existing positions if deleting from the middle! If you don’t want that use insert_at(index, nil)
107 108 109 |
# File 'lib/talia_core/ordered_source.rb', line 107 def delete(index) ordered_objects.delete_at(index) end |
#delete_all ⇒ Object
remove all existing object to ordered source TODO: This will not reliably delete all elements, since the size
value is not reliable
114 115 116 |
# File 'lib/talia_core/ordered_source.rb', line 114 def delete_all @ordered_objects = [] end |
#elements ⇒ Object
Returns all elements (not the relations) in an ordered array. Unlike the ordered_objects accessor this will not include nil elements and the index will not have a 1:1 relation to the position of the elment.
The ordering of the elements will be preserved, though.
30 31 32 33 |
# File 'lib/talia_core/ordered_source.rb', line 30 def elements # execute query ordered_objects.compact end |
#find_position_by_object(object) ⇒ Object
Find the position index of the given object. This will always find the first occurence
127 128 129 |
# File 'lib/talia_core/ordered_source.rb', line 127 def find_position_by_object(object) ordered_objects.index(object) end |
#first ⇒ Object
Returns the first element of the collection
36 37 38 |
# File 'lib/talia_core/ordered_source.rb', line 36 def first ordered_objects.find { |el| !el.nil? } end |
#index_to_predicate(index) ⇒ Object
return string for index
132 133 134 |
# File 'lib/talia_core/ordered_source.rb', line 132 def index_to_predicate(index) self.class.index_to_predicate(index) end |
#insert_at(index, object) ⇒ Object
Inserts an element at the given index.
92 93 94 |
# File 'lib/talia_core/ordered_source.rb', line 92 def insert_at(index, object) write_for_index(ordered_objects, index, object) end |
#next(current_element = nil) ⇒ Object
return next item
-
current_element: int or string. Current element. If nil, the index is the last integer used with at method
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/talia_core/ordered_source.rb', line 51 def next(current_element = nil) set_current_index_for(current_element) # if current element is nil, next must return first value @current_index ||= 0 if (@current_index < (size - 1)) return at(@current_index + 1) # TODO: Current is not increased, is this intentional? Do we need this method at all? else raise "Last item reached" end end |
#ordered_objects ⇒ Object
Returns all the objects that are ordered in an array where the array index equals the position of the object in the ordered set. The array is zero-based, position that don’t have an object attached will be set to nil.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/talia_core/ordered_source.rb', line 145 def ordered_objects return @ordered_objects if(@ordered_objects) relations = query # Let's assume the follwing is a sane assumption ;-) # Even if a one-base collection comes in, we need to push just one element @ordered_objects = Array.new(relations.size) # Now add the elements so that the relation property is reflected # on the position in the array relations.each do |rel| index = rel.rel_order write_for_index(@ordered_objects, index, rel.object) end @ordered_objects end |
#predicate_to_index(predicate) ⇒ Object
return index of predicate
137 138 139 |
# File 'lib/talia_core/ordered_source.rb', line 137 def predicate_to_index(predicate) self.class.predicate_to_index(predicate) end |
#previous(current_element = nil) ⇒ Object
return previous item
-
current_element: int or string. Current element. If nil, the index is the last integer used with at method
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/talia_core/ordered_source.rb', line 66 def previous(current_element = nil) set_current_index_for(current_element) # if current element is nil, next must return first value @current_index = (size + 1) if @current_index.nil? if (@current_index > 1) # TODO: This assumes a one-base array, not really useful return at(@current_index - 1) # TODO: See above else raise "First item reached" end end |
#replace(index, object) ⇒ Object
replace item as position index with object
-
index: int
-
object: TaliaCore::ActiveSource
121 122 123 |
# File 'lib/talia_core/ordered_source.rb', line 121 def replace(index, object) replace_for_index(ordered_objects, index, object) end |
#size ⇒ Object
Return the “size” of the collection. This is actually the maximum position index that is used. The value is cached internally and will be increased on insert_at (only if inserting at a value larger at the current size) and on the add operation. If elements are added in another way, the count may be off, but in that case it would be off anyway. Delete will decrease the size by 1, if the index removed equals the size (this is a rough guess which may be off!) The cached counter will be reset on saving.
87 88 89 |
# File 'lib/talia_core/ordered_source.rb', line 87 def size ordered_objects.size end |