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

.by_areaObject

order by area (in square km)



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

def self.by_area  # order by area (in square km)
  self.order( 'area desc')
end

.by_keyObject

order by key (a-z)



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

def self.by_key  # order by key (a-z)
  self.order( 'key asc' )
end

.by_popObject

order by pop(ulation)



26
27
28
# File 'lib/worlddb/models/city.rb', line 26

def self.by_pop  # order by pop(ulation)
  self.order( 'pop desc' )
end

.by_titleObject

order by title (a-z)



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

def self.by_title  # order by title (a-z)
  self.order( 'title asc' )
end

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/worlddb/models/city.rb', line 45

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

#title_w_synonymsObject



34
35
36
37
38
39
40
41
42
# File 'lib/worlddb/models/city.rb', line 34

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