Class: Uncharted::Territory
- Inherits:
-
Object
- Object
- Uncharted::Territory
- Includes:
- Mongoid::Fields::Serializable
- Defined in:
- lib/uncharted/territory.rb,
lib/uncharted/extensions/mongoid.rb
Constant Summary collapse
- TYPES =
[:administration, :atol, :authority, :borough, :canton, :capital, :city, :collectivity, :commune, :council, :county, :department, :dependency, :district, :emirate, :entity, :governorate, :island, :metropolis, :municipality, :parish, :part, :prefecture, :province, :region, :republic, :sector, :state, :territory, :unit, :voivodship, :zone]
Instance Attribute Summary collapse
-
#abbr ⇒ Object
readonly
Returns the value of attribute abbr.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .data ⇒ Object
- .find(object) ⇒ Object
- .find_by_name(name) ⇒ Object
-
.load(path = "data/territories.csv", options = {:encoding => "UTF-8", :locale => 'pt-BR'}) ⇒ Object
Columns are expected in this order 1.
- .search(object) ⇒ Object
Instance Method Summary collapse
- #deserialize(code) ⇒ Object
-
#initialize(code, name, type) ⇒ Territory
constructor
A new instance of Territory.
- #inspect ⇒ Object
- #name(options = {}) ⇒ Object
- #names ⇒ Object
- #serialize(object) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(code, name, type) ⇒ Territory
Returns a new instance of Territory.
40 41 42 43 44 45 46 47 48 |
# File 'lib/uncharted/territory.rb', line 40 def initialize(code, name, type) @code = code @name = name @country_code, @abbr = code.split('-') @country = Country.find(@country_code) Territory.data[code] = self @country.subdivisions << self if @country @type = TYPES.detect {|t| /#{t}/ =~ type} || :territory end |
Instance Attribute Details
#abbr ⇒ Object (readonly)
Returns the value of attribute abbr.
34 35 36 |
# File 'lib/uncharted/territory.rb', line 34 def abbr @abbr end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
34 35 36 |
# File 'lib/uncharted/territory.rb', line 34 def code @code end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
34 35 36 |
# File 'lib/uncharted/territory.rb', line 34 def country @country end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
34 35 36 |
# File 'lib/uncharted/territory.rb', line 34 def country_code @country_code end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
34 35 36 |
# File 'lib/uncharted/territory.rb', line 34 def type @type end |
Class Method Details
.data ⇒ Object
70 71 72 |
# File 'lib/uncharted/territory.rb', line 70 def self.data @data ||= {} end |
.find(object) ⇒ Object
66 67 68 |
# File 'lib/uncharted/territory.rb', line 66 def self.find(object) territory = object && object.is_a?(Territory) ? object : data[object] end |
.find_by_name(name) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/uncharted/territory.rb', line 74 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 |
.load(path = "data/territories.csv", options = {:encoding => "UTF-8", :locale => 'pt-BR'}) ⇒ Object
Columns are expected in this order
-
Territory ISO 3166-2 code
-
Territory name
-
Territory level name (state, territory, parish, etc)
95 96 97 98 99 100 101 102 103 |
# File 'lib/uncharted/territory.rb', line 95 def self.load(path = "data/territories.csv", = {:encoding => "UTF-8", :locale => 'pt-BR'}) CSV.foreach(path, :encoding => [:encoding]) do |row| code, name, type = row.collect {|c| c && c.strip} type && type.downcase! # puts "Importing: #{code} :: #{name} :: #{type}" new(code, name, type) if code =~ /^[A-Z]{2}-[A-Z0-9]{1,3}$/ && name && name.size > 0 end #Territory.data.each {|code, t| puts t.inspect} end |
.search(object) ⇒ Object
86 87 88 |
# File 'lib/uncharted/territory.rb', line 86 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 |
#inspect ⇒ Object
62 63 64 |
# File 'lib/uncharted/territory.rb', line 62 def inspect "#{@code} - #{@name} (#{@type} - #{@country.inspect})" end |
#name(options = {}) ⇒ Object
50 51 52 |
# File 'lib/uncharted/territory.rb', line 50 def name( = {}) I18n.t("territories.#{@country_code}#{@abbr}", {locale: [:locale] || I18n.locale}, default: @name) end |
#names ⇒ Object
54 55 56 |
# File 'lib/uncharted/territory.rb', line 54 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_s ⇒ Object
58 59 60 |
# File 'lib/uncharted/territory.rb', line 58 def to_s @abbr end |