class Taxonifi::Model::Collection

The base class that all collection classes are derived from.

Attributes

by_id_index[RW]
collection[RW]
current_free_id[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

add_object(obj) click to toggle source

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_object_pre_indexed(obj) click to toggle source

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
object_by_id(id) click to toggle source

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
object_class() click to toggle source

Define the default class. Over-ridden in specific collections.

# File lib/models/collection.rb, line 24
def object_class
  Taxonifi::Model::GenericObject
end
objects_without_parents() click to toggle source

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
parent_id_vector(id = Fixnum) click to toggle source

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

Protected Instance Methods

object_is_allowed?(obj) click to toggle source

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