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.



24
25
26
# File 'lib/taxonifi/model/base.rb', line 24

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

Instance Attribute Details

#idObject

The id of this object.



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

def id
  @id
end

#propertiesObject

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_numberObject

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_idsObject

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

#ancestorsObject

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