The base class that all collection classes are derived from.
# File lib/models/collection.rb, line 11 def initialize(options = {}) opts = { :initial_id => 0 }.merge!(options) raise CollectionError, "Can not start with an initial_id of nil." if opts[:initial_id].nil? @collection = [] @by_id_index = {} @current_free_id = opts[:initial_id] true end
Add an object to the collection.
# File lib/models/collection.rb, line 34 def add_object(obj) raise CollectionError, "Taxonifi::Model::#{object_class.class}#id may not be pre-initialized if used with #add_object, consider using #add_object_pre_indexed." if !obj.id.nil? object_is_allowed?(obj) obj.id = @current_free_id.to_i @current_free_id += 1 @collection.push(obj) @by_id_index.merge!(obj.id => obj) return obj end
Add an object without setting its ID.
# File lib/models/collection.rb, line 45 def add_object_pre_indexed(obj) object_is_allowed?(obj) raise CollectionError, "Taxonifi::Model::#{object_class.class} does not have a pre-indexed id." if obj.id.nil? @collection.push(obj) @by_id_index.merge!(obj.id => obj) return obj end
Return an object in this collection by id.
# File lib/models/collection.rb, line 29 def object_by_id(id) @by_id_index[id] end
Define the default class. Over-ridden in specific collections.
# File lib/models/collection.rb, line 24 def object_class Taxonifi::Model::GenericObject end
Returns an Array which respresents all the “root” objects.
# File lib/models/collection.rb, line 77 def objects_without_parents collection.select{|o| o.parent.nil?} end
Return an array of ancestor (parent) ids. TODO: deprecate? More or less identical to Taxonifi::Name.ancestor_ids except this checks against the indexed names in the collection rather than Name->Name relationships The two should be identical in all(?) conievable cases
# File lib/models/collection.rb, line 60 def parent_id_vector(id = Fixnum) vector = [] return vector if @by_id_index[id].nil? || @by_id_index[id].parent.nil? id = @by_id_index[id].parent.id while !id.nil? vector.unshift id if @by_id_index[id].parent id = @by_id_index[id].parent.id else id = nil end end vector end
Check to see that the object can be added to this collection.
# File lib/models/collection.rb, line 84 def object_is_allowed?(obj) raise CollectionError, "Taxonifi::Model::#{object_class.class} not passed to Collection.add_object()." if !(obj.class == object_class) true end