Class: Taxonifi::Model::Base
- Inherits:
-
Object
- Object
- Taxonifi::Model::Base
- Includes:
- SharedClassMethods
- Defined in:
- lib/taxonifi/model/base.rb
Overview
A base class for all Taxonifi::Models that represent “individuals” (as opposed to collections of indviduals).
Direct Known Subclasses
AuthorYear, GenericObject, Geog, Name, Person, Ref, SpeciesName
Constant Summary collapse
- ATTRIBUTES =
TODO: Rethink this. See @@ATTRIBUTES in subclasses.
[:row_number]
Instance Attribute Summary collapse
-
#id ⇒ Object
The id of this object.
-
#properties ⇒ Object
A general purpose Hash populable as needed for related metadata.
-
#row_number ⇒ Object
Optionly store the row this came from.
Instance Method Summary collapse
-
#add_properties(hash) ⇒ Object
Add a set of properties (doesn’t check for key collisions).
-
#add_property(key, value) ⇒ Object
Add a key/value pair to @properties.
-
#ancestor_ids ⇒ Object
The ids only of ancestors.
-
#ancestors ⇒ Object
Ancestor objects for subclasses that have a parent property.
-
#build(attributes, opts) ⇒ Object
Assign on new() all attributes for the ATTRIBUTES constant in a given subclass.
-
#delete_property(key) ⇒ Object
Delete an existing key/value pair in @properties.
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
-
#replace_property(key, value) ⇒ Object
Replace an existing key/value pair in @properties.
Methods included from SharedClassMethods
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
24 25 26 |
# File 'lib/taxonifi/model/base.rb', line 24 def initialize( = {}) @properties = {} end |
Instance Attribute Details
#id ⇒ Object
The id of this object.
13 14 15 |
# File 'lib/taxonifi/model/base.rb', line 13 def id @id end |
#properties ⇒ Object
A general purpose Hash populable as needed for related metadata
19 20 21 |
# File 'lib/taxonifi/model/base.rb', line 19 def properties @properties end |
#row_number ⇒ Object
Optionly store the row this came from
16 17 18 |
# File 'lib/taxonifi/model/base.rb', line 16 def row_number @row_number end |
Instance Method Details
#add_properties(hash) ⇒ Object
Add a set of properties (doesn’t check for key collisions)
38 39 40 |
# File 'lib/taxonifi/model/base.rb', line 38 def add_properties(hash) @properties.merge!(hash) end |
#add_property(key, value) ⇒ Object
Add a key/value pair to @properties
43 44 45 46 47 48 49 |
# File 'lib/taxonifi/model/base.rb', line 43 def add_property(key, value) if @properties[key] return false else @properties.merge!(key => value) end end |
#ancestor_ids ⇒ Object
The ids only of ancestors. Immediate ancestor id is in [].last
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/taxonifi/model/base.rb', line 76 def ancestor_ids i = 0 # check for recursion ids = [] p = parent while !p.nil? ids.unshift p.id p = p.parent i += 1 raise Taxonifi::ModelError, "Infite recursion in parent string detected for Base model object #{id}." if i > 100 end ids end |
#ancestors ⇒ Object
Ancestor objects for subclasses that have a parent property. TODO: check for parent attributes
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/taxonifi/model/base.rb', line 92 def ancestors i = 0 # check for recursion ancestors = [] p = parent while !p.nil? ancestors.unshift p p = p.parent i += 1 raise Taxonifi::ModelError, "Infite recursion in parent string detected for Base model object #{id.display_name}." if i > 100 end ancestors end |
#build(attributes, opts) ⇒ Object
Assign on new() all attributes for the ATTRIBUTES constant in a given subclass. !! Check validity prior to building.
31 32 33 34 35 |
# File 'lib/taxonifi/model/base.rb', line 31 def build(attributes, opts) attributes.each do |c| self.send("#{c}=",opts[c]) if !opts[c].nil? end end |
#delete_property(key) ⇒ Object
Delete an existing key/value pair in @properties
61 62 63 64 65 66 67 |
# File 'lib/taxonifi/model/base.rb', line 61 def delete_property(key) if !@properties[key] @properties.delete(key) else @properties.merge!(key => value) end end |
#replace_property(key, value) ⇒ Object
Replace an existing key/value pair in @properties
52 53 54 55 56 57 58 |
# File 'lib/taxonifi/model/base.rb', line 52 def replace_property(key,value) if !@properties[key] return false else @properties.merge!(key => value) end end |