Class: Uncharted::Country
- Inherits:
-
Object
- Object
- Uncharted::Country
- Includes:
- Mongoid::Fields::Serializable
- Defined in:
- lib/uncharted/country.rb,
lib/uncharted/territory.rb,
lib/uncharted/extensions/mongoid.rb
Instance Attribute Summary collapse
-
#alpha2 ⇒ Object
(also: #code)
readonly
Returns the value of attribute alpha2.
-
#alpha3 ⇒ Object
readonly
Returns the value of attribute alpha3.
-
#name(options = {}) ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .count ⇒ Object
- .countries ⇒ Object
- .data ⇒ Object
- .find(objects) ⇒ Object
- .find_by_name(name) ⇒ Object
- .search(object) ⇒ Object
- .split_name(name) ⇒ Object
- .subdivisions ⇒ Object
Instance Method Summary collapse
- #deserialize(code) ⇒ Object
- #districts ⇒ Object
- #find_by_type(type) ⇒ Object
-
#initialize(alpha2, alpha3 = nil, name = nil) ⇒ Country
constructor
A new instance of Country.
- #inspect ⇒ Object
- #names ⇒ Object
- #serialize(object) ⇒ Object
- #states ⇒ Object
- #subdivisions ⇒ Object
- #territories ⇒ Object
- #to_s ⇒ Object
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
#alpha2 ⇒ Object (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 |
#alpha3 ⇒ Object (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
.count ⇒ Object
32 33 34 |
# File 'lib/uncharted/country.rb', line 32 def self.count countries.count end |
.countries ⇒ Object
66 67 68 |
# File 'lib/uncharted/country.rb', line 66 def self.countries @data.values end |
.data ⇒ Object
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 |
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 |
#districts ⇒ Object
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 |
#inspect ⇒ Object
16 17 18 |
# File 'lib/uncharted/country.rb', line 16 def inspect "#{@alpha2}: '#{@name}'" end |
#names ⇒ Object
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 |
#states ⇒ Object
14 15 16 |
# File 'lib/uncharted/territory.rb', line 14 def states @states ||= find_by_type(:state) end |
#subdivisions ⇒ Object
6 7 8 |
# File 'lib/uncharted/territory.rb', line 6 def subdivisions @subdivisions ||= [] end |
#territories ⇒ Object
10 11 12 |
# File 'lib/uncharted/territory.rb', line 10 def territories @territories ||= find_by_type(:territory) end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/uncharted/country.rb', line 28 def to_s @alpha2 end |