Class: Taxonifi::Model::Base

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from SharedClassMethods

included

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



22
23
24
# File 'lib/taxonifi/model/base.rb', line 22

def initialize(options = {})
  @properties = {}
end

Instance Attribute Details

#idObject

The id of this object.



11
12
13
# File 'lib/taxonifi/model/base.rb', line 11

def id
  @id
end

#propertiesObject

A general purpose Hash populable as needed for related metadata



17
18
19
# File 'lib/taxonifi/model/base.rb', line 17

def properties
  @properties
end

#row_numberObject

Optionly store the row this came from



14
15
16
# File 'lib/taxonifi/model/base.rb', line 14

def row_number
  @row_number
end

Instance Method Details

#add_properties(hash) ⇒ Object

Add a set of properties (doesn’t check for key collisions)



36
37
38
# File 'lib/taxonifi/model/base.rb', line 36

def add_properties(hash)
  @properties.merge!(hash)
end

#add_property(key, value) ⇒ Object

Add a key/value pair to @properties



41
42
43
44
45
46
47
# File 'lib/taxonifi/model/base.rb', line 41

def add_property(key, value)
  if @properties[key] 
    return false
  else
    @properties.merge!(key => value)
  end
end

#ancestor_idsObject

The ids only of ancestors. Immediate ancestor id is in [].last



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/taxonifi/model/base.rb', line 74

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

#ancestorsObject

Ancestor objects for subclasses that have a parent property. TODO: check for parent attributes



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/taxonifi/model/base.rb', line 90

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.



29
30
31
32
33
# File 'lib/taxonifi/model/base.rb', line 29

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



59
60
61
62
63
64
65
# File 'lib/taxonifi/model/base.rb', line 59

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



50
51
52
53
54
55
56
# File 'lib/taxonifi/model/base.rb', line 50

def replace_property(key,value)
 if !@properties[key]
  return false
 else
  @properties.merge!(key => value)
 end
end