Class: Taxonifi::Model::Collection
- Inherits:
-
Object
- Object
- Taxonifi::Model::Collection
- Includes:
- SharedClassMethods
- Defined in:
- lib/taxonifi/model/collection.rb
Overview
The base class that all collection classes are derived from.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#by_id_index ⇒ Object
A Hash indexing object by id like => SomeBaseSubclass.
-
#collection ⇒ Object
An Array, the collection.
-
#current_free_id ⇒ Object
A Integer representing the current free id to be used for an accessioned a collection object.
Class Method Summary collapse
-
.subclass_prefixes ⇒ Object
Returns an array of (downcased) strings representing the prefixes of the Collection based subclasses, like [‘name’, ‘geog’, ‘ref’] etc.
Instance Method Summary collapse
-
#add_object(obj) ⇒ Object
Add an object to the collection.
-
#add_object_pre_indexed(obj) ⇒ Object
Add an object without setting its ID.
-
#children_of_object(o) ⇒ Object
Returns an Array of immediate children TODO: test.
-
#initialize(options = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#object_by_id(id) ⇒ Object
Return an object in this collection by id.
-
#object_class ⇒ Object
Define the default class.
-
#objects_without_parents ⇒ Object
Returns an Array which respresents all the “root” objects.
-
#parent_id_vector(id = Fixnum) ⇒ Object
Return an array of ancestor (parent) ids.
Methods included from SharedClassMethods
Constructor Details
#initialize(options = {}) ⇒ Collection
Returns a new instance of Collection.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/taxonifi/model/collection.rb', line 26 def initialize( = {}) opts = { :initial_id => 0 }.merge!() raise CollectionError, "Can not start with an initial_id of nil." if opts[:initial_id].nil? @collection = [] @by_id_index = {} # @by_row_index = {} @current_free_id = opts[:initial_id] true end |
Instance Attribute Details
#by_id_index ⇒ Object
A Hash indexing object by id like => SomeBaseSubclass
12 13 14 |
# File 'lib/taxonifi/model/collection.rb', line 12 def by_id_index @by_id_index end |
#collection ⇒ Object
An Array, the collection.
18 19 20 |
# File 'lib/taxonifi/model/collection.rb', line 18 def collection @collection end |
#current_free_id ⇒ Object
A Integer representing the current free id to be used for an accessioned a collection object. Not used in non-indexed collections.
15 16 17 |
# File 'lib/taxonifi/model/collection.rb', line 15 def current_free_id @current_free_id end |
Class Method Details
.subclass_prefixes ⇒ Object
Returns an array of (downcased) strings representing the prefixes of the Collection based subclasses, like
- ‘name’, ‘geog’, ‘ref’
-
etc.
22 23 24 |
# File 'lib/taxonifi/model/collection.rb', line 22 def self.subclass_prefixes self.subclasses.collect{|c| c.to_s.split("::").last}.collect{|n| n.gsub(/Collection/, "").downcase} end |
Instance Method Details
#add_object(obj) ⇒ Object
Add an object to the collection.
50 51 52 53 54 55 56 57 58 |
# File 'lib/taxonifi/model/collection.rb', line 50 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) ⇒ Object
Add an object without setting its ID.
61 62 63 64 65 66 67 |
# File 'lib/taxonifi/model/collection.rb', line 61 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 |
#children_of_object(o) ⇒ Object
Returns an Array of immediate children TODO: test
99 100 101 |
# File 'lib/taxonifi/model/collection.rb', line 99 def children_of_object(o) @collection.select{|i| i.parent == o} end |
#object_by_id(id) ⇒ Object
Return an object in this collection by id.
45 46 47 |
# File 'lib/taxonifi/model/collection.rb', line 45 def object_by_id(id) @by_id_index[id] end |
#object_class ⇒ Object
Define the default class. Over-ridden in specific collections.
40 41 42 |
# File 'lib/taxonifi/model/collection.rb', line 40 def object_class Taxonifi::Model::GenericObject end |
#objects_without_parents ⇒ Object
Returns an Array which respresents all the “root” objects. TODO: test
93 94 95 |
# File 'lib/taxonifi/model/collection.rb', line 93 def objects_without_parents @collection.select{|o| o.parent.nil?} end |
#parent_id_vector(id = Fixnum) ⇒ Object
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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/taxonifi/model/collection.rb', line 75 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 |