Class: WorldDb::Models::City

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/worlddb/models/city.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_ary!(cities, more_values = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/worlddb/models/city.rb', line 65

def self.create_from_ary!( cities, more_values={} )
  cities.each do |values|
    
    ## key & title & country required
    attr = {
      key: values[0]
    }
    
    ## title (split of optional synonyms)
    # e.g. FC Bayern Muenchen|Bayern Muenchen|Bayern
    titles = values[1].split('|')
    
    attr[ :title ]    =  titles[0]
    ## add optional synonyms
    attr[ :synonyms ] =  titles[1..-1].join('|')  if titles.size > 1
    
    attr = attr.merge( more_values )
    
    value_numbers = []
    
    ## check for optional values
    values[2..-1].each do |value|
      if value.is_a? Country
        attr[ :country_id ] = value.id
      elsif value.is_a? Numeric
        value_numbers << value
      elsif value =~ /^[A-Z_]{3}$/    ## assume its three letter code (e.g. NYC,VIE,etc.)
        attr[ :code ] = value
      elsif value =~ /^region:/   ## region:
        value_region_key = value[7..-1]  ## cut off region: prefix
        value_region = Region.find_by_key!( value_region_key )
        attr[ :region_id ] = value_region.id
      else
        # issue warning: unknown type for value
      end
    end
    
    if value_numbers.size > 0
      attr[ :pop  ] = value_numbers[0]
      attr[ :area ] = value_numbers[1]
    end

    
    City.create!( attr )
  end # each city
end

Instance Method Details

#citiesObject

(1) metro - level up



16
# File 'lib/worlddb/models/city.rb', line 16

has_many   :cities,    :class_name => 'City', :foreign_key => 'city_id'

#cityObject

(3) district - level down



23
# File 'lib/worlddb/models/city.rb', line 23

belongs_to :city,      :class_name => 'City', :foreign_key => 'city_id'

#is_city?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/worlddb/models/city.rb', line 33

def is_city?
  c? == true
end

#is_district?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/worlddb/models/city.rb', line 37

def is_district?
  d? == true
end

#is_metro?Boolean

NB: use is_ for flags to avoid conflict w/ assocs (e.g. metro?, city? etc.)

Returns:

  • (Boolean)


29
30
31
# File 'lib/worlddb/models/city.rb', line 29

def is_metro?
  m? == true
end

#metroObject

(2) city



19
# File 'lib/worlddb/models/city.rb', line 19

belongs_to :metro,     :class_name => 'City', :foreign_key => 'city_id'

#title_w_synonymsObject



54
55
56
57
58
59
60
61
62
# File 'lib/worlddb/models/city.rb', line 54

def title_w_synonyms
  return title if synonyms.blank?
  
  buf = ''
  buf << title
  buf << ' | '
  buf << synonyms.split('|').join(' | ')
  buf
end