Class: Uncharted::Country

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Fields::Serializable
Defined in:
lib/uncharted/country.rb,
lib/uncharted/territory.rb,
lib/uncharted/extensions/mongoid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alpha2, alpha3 = nil, name = nil) ⇒ Country

Returns a new instance of Country.



9
10
11
12
13
14
# File 'lib/uncharted/country.rb', line 9

def initialize(alpha2, alpha3 = nil, name = nil)
  @alpha2 = alpha2
  @alpha3 = alpha3
  @name   = name
  self.class.data[alpha2] = self
end

Instance Attribute Details

#alpha2Object (readonly) Also known as: code

Returns the value of attribute alpha2.



6
7
8
# File 'lib/uncharted/country.rb', line 6

def alpha2
  @alpha2
end

#alpha3Object (readonly)

Returns the value of attribute alpha3.



6
7
8
# File 'lib/uncharted/country.rb', line 6

def alpha3
  @alpha3
end

#name(options = {}) ⇒ Object (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/uncharted/country.rb', line 6

def name
  @name
end

Class Method Details

.countObject



32
33
34
# File 'lib/uncharted/country.rb', line 32

def self.count
  countries.count
end

.countriesObject



66
67
68
# File 'lib/uncharted/country.rb', line 66

def self.countries
  @data.values
end

.dataObject



70
71
72
# File 'lib/uncharted/country.rb', line 70

def self.data
  @data ||= {}
end

.find(objects) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/uncharted/country.rb', line 36

def self.find(objects)
  case objects
  when Array
    objects.collect {|c| data[c]}
  when Country, nil
    objects
  else # code
    code = objects.to_s
    code.upcase!
    data[code]
  end
end

.find_by_name(name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/uncharted/country.rb', line 49

def self.find_by_name(name)
  return nil if name.nil? || name == ""
  closest = [nil, 0]
  @data.each do |code, country|
    return country if country.name == name
    intersection = country.names & Country.split_name(name)
    size = intersection.size
    return country if size == country.names.size
    closest = [country, size] if closest.last < size 
  end
  return closest.first
end

.search(object) ⇒ Object



62
63
64
# File 'lib/uncharted/country.rb', line 62

def self.search(object)
  find(object) || find_by_name(object)
end

.split_name(name) ⇒ Object



74
75
76
77
78
# File 'lib/uncharted/country.rb', line 74

def self.split_name(name)
  name = I18n.transliterate(name)
  name.downcase!
  name.split
end

.subdivisionsObject



22
23
24
# File 'lib/uncharted/territory.rb', line 22

def self.subdivisions
  Territory.data
end

Instance Method Details

#deserialize(code) ⇒ Object



8
9
10
# File 'lib/uncharted/extensions/mongoid.rb', line 8

def deserialize(code)
  code && Country.find(code)
end

#districtsObject



18
19
20
# File 'lib/uncharted/territory.rb', line 18

def districts
  @districts ||= find_by_type(:district)
end

#find_by_type(type) ⇒ Object



26
27
28
# File 'lib/uncharted/territory.rb', line 26

def find_by_type(type)
  subdivisions.select {|t| t.type == type}
end

#inspectObject



16
17
18
# File 'lib/uncharted/country.rb', line 16

def inspect
  "#{@alpha2}: '#{@name}'"
end

#namesObject



24
25
26
# File 'lib/uncharted/country.rb', line 24

def names
  @names ||= Country.split_name(@name)
end

#serialize(object) ⇒ Object



12
13
14
15
# File 'lib/uncharted/extensions/mongoid.rb', line 12

def serialize(object)
  country = object.is_a?(Country) ? object : Country.search(object)
  country && country.code
end

#statesObject



14
15
16
# File 'lib/uncharted/territory.rb', line 14

def states
  @states ||= find_by_type(:state)
end

#subdivisionsObject



6
7
8
# File 'lib/uncharted/territory.rb', line 6

def subdivisions
  @subdivisions ||= []
end

#territoriesObject



10
11
12
# File 'lib/uncharted/territory.rb', line 10

def territories
  @territories ||= find_by_type(:territory)
end

#to_sObject



28
29
30
# File 'lib/uncharted/country.rb', line 28

def to_s
  @alpha2
end