Class TaliaCore::Collection
In: lib/talia_core/collection.rb
Parent: Source

Represents a collection of sources. In addition to being a container for sources, the Collection class will also provide an ordering of the contained sources.

In a nutshell, this behaves like an array of sources that preserves the order when saved.

The ordering will always assign a ’’unique’’ integer value to each contained source that defines its position in the order of elements. The collection will keep an internal array where each object‘s index maps directly to its position in the collection; the array and ordering are saved to the data store when the collection itself is saved.

The collection class is relatively lightweight and will behave mostly like the underlying array - most operations are simply passed through to the array and nothing is saved before the collection itself is saved.

Operations that are passed to the underlying array are: +, <<, ==, []=, at, clear, collect, delete_at, delete, each, each_index, empty?, include?, index, join, last, length and size.

This also means that all checks on added objects will only be performed when the collection is saved, and not much checking is done when the array is modified.

In the RDF, the collection is represented as a seqContainer, using a predicate of "www.w3.org/1999/02/22-rdf-syntax-ns#_[index of element x]" to connect an element x with the collection.

Note: This class replaces the previous OrderedSource class

Methods

Included Modules

Enumerable

Public Class methods

Returns the predicate that will be used for the collection element with the given index. The result will be:

  http://www.w3.org/1999/02/22-rdf-syntax-ns#_<index>

[Source]

     # File lib/talia_core/collection.rb, line 109
109:     def self.index_to_predicate(index)
110:       'http://www.w3.org/1999/02/22-rdf-syntax-ns#_' << ("%06d" % index.to_i) 
111:     end

Creates a new Collection. Takes the same parameters as ActiveSource.new

[Source]

    # File lib/talia_core/collection.rb, line 48
48:     def self.new(*params)
49:       collection = super(*params)
50:       collection.autosave_rdf = false # Will do this by ourselves
51:       collection
52:     end

Takes a predicate of the form produced by index_to_predicate and returns the numeric index of the element

[Source]

     # File lib/talia_core/collection.rb, line 115
115:     def self.predicate_to_index(predicate)
116:       predicate.sub('http://www.w3.org/1999/02/22-rdf-syntax-ns#_', '').to_i
117:     end

Public Instance methods

This accessor can be used for both collection items and predicates. If a number is passed in, the object will behave like an Array and the source at the given index is returned. Otherwise the parameter is treated like a predicate and it behaves like ActiveSource#[].

[Source]

    # File lib/talia_core/collection.rb, line 69
69:     def [](index_or_predicate)
70:       if(index_or_predicate.is_a?(Fixnum))
71:         ordered_objects[index_or_predicate]
72:       else
73:         super
74:       end
75:     end

Writer that behaves in the same way as []

[Source]

    # File lib/talia_core/collection.rb, line 78
78:     def []=(index_or_predicate, value)
79:       if(index_or_predicate.is_a?(Fixnum))
80:         ordered_objects[index_or_predicate] = value
81:       else
82:         super
83:       end
84:     end

Returns all contained sources in an ordered array.

The contained sources will appear in the sequential order in which they are contained in the collection, but there is no direct relation between the index in the collection and the index returned through this method.

[Source]

    # File lib/talia_core/collection.rb, line 91
91:     def elements
92:       # execute query
93:       ordered_objects.compact
94:     end

See Collection.index_to_predicate

[Source]

    # File lib/talia_core/collection.rb, line 97
97:     def index_to_predicate(index)
98:       self.class.index_to_predicate(index)
99:     end

See Collection.predicate_to_index

[Source]

     # File lib/talia_core/collection.rb, line 102
102:     def predicate_to_index(predicate)
103:       self.class.predicate_to_index(predicate)
104:     end

[Validate]