Class: Uncharted::Territory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, type, name) ⇒ Territory

Returns a new instance of Territory.



36
37
38
39
40
41
42
43
44
# File 'lib/uncharted/territory.rb', line 36

def initialize(code, type, name)
  @code = code
  @type = type
  @name = name
  @country_code, @abbr = code.split('-')
  @country = Country.find(@country_code)
  Territory.data[code] = self
  @country.subdivisions << self if @country
end

Instance Attribute Details

#abbrObject (readonly)

Returns the value of attribute abbr.



34
35
36
# File 'lib/uncharted/territory.rb', line 34

def abbr
  @abbr
end

#codeObject (readonly)

Returns the value of attribute code.



34
35
36
# File 'lib/uncharted/territory.rb', line 34

def code
  @code
end

#countryObject (readonly)

Returns the value of attribute country.



34
35
36
# File 'lib/uncharted/territory.rb', line 34

def country
  @country
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



34
35
36
# File 'lib/uncharted/territory.rb', line 34

def country_code
  @country_code
end

#typeObject (readonly)

Returns the value of attribute type.



34
35
36
# File 'lib/uncharted/territory.rb', line 34

def type
  @type
end

Class Method Details

.dataObject



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

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

.find(object) ⇒ Object



58
59
60
# File 'lib/uncharted/territory.rb', line 58

def self.find(object)
  territory = object && object.is_a?(Territory) ? object : data[object]
end

.find_by_name(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/uncharted/territory.rb', line 66

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

.search(object) ⇒ Object



78
79
80
# File 'lib/uncharted/territory.rb', line 78

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

Instance Method Details

#deserialize(code) ⇒ Object



21
22
23
# File 'lib/uncharted/extensions/mongoid.rb', line 21

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

#name(options = {}) ⇒ Object



46
47
48
# File 'lib/uncharted/territory.rb', line 46

def name(options = {})
  I18n.t("territories.#{@country_code}#{@abbr}", {locale: options[:locale] || I18n.locale}, default: @name)
end

#namesObject



50
51
52
# File 'lib/uncharted/territory.rb', line 50

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

#serialize(object) ⇒ Object



25
26
27
28
# File 'lib/uncharted/extensions/mongoid.rb', line 25

def serialize(object)
  territory = object.is_a?(Territory) ? object : Territory.search(object) 
  territory && territory.code
end

#to_sObject



54
55
56
# File 'lib/uncharted/territory.rb', line 54

def to_s
  @abbr
end