Class: Taxonifi::Model::Geog

Inherits:
Base
  • Object
show all
Defined in:
lib/taxonifi/model/geog.rb

Overview

An instance of some geopolitical unit. Not fully developed yet.

Constant Summary collapse

GEOG_RANKS =

require ‘geokit’ include Geokit::Geocoders

['country', 'state', 'county']
ATTRIBUTES =
[:name, :rank, :parent]

Instance Attribute Summary

Attributes inherited from Base

#id, #properties, #row_number

Instance Method Summary collapse

Methods inherited from Base

#add_properties, #add_property, #ancestor_ids, #ancestors, #build, #delete_property, #replace_property

Methods included from SharedClassMethods

included

Constructor Details

#initialize(options = {}) ⇒ Geog

Returns a new instance of Geog.



18
19
20
21
22
23
24
25
# File 'lib/taxonifi/model/geog.rb', line 18

def initialize(options = {})
  opts = {
  }.merge!(options)
  @parent = nil
  build(ATTRIBUTES - [:parent], opts)
  @parent = opts[:parent] if (!opts[:parent].nil? && opts[:parent].class == Taxonifi::Model::Geog)
  true
end

Instance Method Details

#parent=(parent) ⇒ Object

Set parent of this rank (also a Taxonifi::Model::Geog instance).



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/taxonifi/model/geog.rb', line 37

def parent=(parent)
 if parent.nil?
   raise GeogError, "Parent can't be set to nil in Taxonifi::Model::Geog."
 end

  if @rank.nil?
    raise Taxonifi::GeogError, "Parent of geog can not be set if rank of child is not set." 
  end

  if parent.class != Taxonifi::Model::Geog
    raise GeogError, "Parent is not a Taxonifi::Model::Geog."
  end

  if GEOG_RANKS.index(parent.rank) >= GEOG_RANKS.index(self.rank)
    raise GeogError, "Parent is same or lower rank than self (#{rank})."
  end
  
  @parent = parent
end

#rank=(rank) ⇒ Object

Set the “rank” of this geographic unit.



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

def rank=(rank)
  r = rank.to_s.downcase.strip
  if !GEOG_RANKS.include?(r) 
    raise GeogError, "#{r} is not a valid rank."
  end
  @rank = r
end