class Taxonifi::Model::RefCollection

A collection of references.

Attributes

author_index[RW]

Points a Taxonifi::Model::Base#id to an array of Person#ids.

Built on request.

row_index[RW]

An options index when there is one reference per row.

Public Class Methods

new(options = {}) click to toggle source
# File lib/models/ref_collection.rb, line 16
def initialize(options = {})
  super
  @row_index = []
  @author_index = {}
  true
end

Public Instance Methods

build_author_index() click to toggle source

Build the author index.

{Ref#id => [a1#id, ... an#id]}
# File lib/models/ref_collection.rb, line 65
def build_author_index
  collection.each do |r|
    @author_index.merge!(r.id => r.authors.collect{|a| a.id ? a.id : -1})
  end
end
enumerate_authors(initial_id = 0) click to toggle source

Incrementally (re-)assigns the id of every associated author (Person) This is only really useful if you assume every author is unique.

# File lib/models/ref_collection.rb, line 36
def enumerate_authors(initial_id = 0)
  i = initial_id 
  collection.each do |r|
    r.authors.each do |a|
      a.id = i
      i += 1
    end
  end
end
object_class() click to toggle source

The instance collection class.

# File lib/models/ref_collection.rb, line 24
def object_class
  Taxonifi::Model::Ref  
end
object_from_row(row_number) click to toggle source

The object at a given row. TODO: inherit from Collection?

# File lib/models/ref_collection.rb, line 30
def object_from_row(row_number)
  @row_index[row_number]
end
unique_author_strings() click to toggle source

Return an array the unique author strings in this collection.

# File lib/models/ref_collection.rb, line 72
def unique_author_strings
  auths = {}
  collection.each do |r|
    r.authors.each do |a|
      auths.merge!(a.display_name => nil)
    end
  end
  auths.keys.sort
end
unique_authors() click to toggle source

Returns Array of Taxonifi::Model::Person Will need better indexing on big lists?

# File lib/models/ref_collection.rb, line 84
def unique_authors
  auths = []
  collection.each do |r|
    r.authors.each do |a|
      found = false
      auths.each do |x|
        if a.identical?(x)
          found = true 
          next           
        end
      end
      if not found
        auths.push a.clone
      end
    end
  end
  auths
end
uniquify_authors(initial_id = 0) click to toggle source

Finds unique authors, and combines them, then rebuilds author lists using references to the new unique set.

# File lib/models/ref_collection.rb, line 48
def uniquify_authors(initial_id = 0)
  auth_index = {}
  unique_authors.each_with_index do |a, i|
    a.id = i + initial_id
    auth_index.merge!(a.compact_string => a)
  end
  
  collection.each do |r|
    new_authors = []
    r.authors.inject(new_authors){|ary, a| ary.push(auth_index[a.compact_string])}
    r.authors = new_authors
  end
  true 
end