Module: ChinaCity

Defined in:
lib/china_city.rb,
lib/china_city/engine.rb,
lib/china_city/version.rb,
app/helpers/china_city/application_helper.rb,
app/controllers/china_city/data_controller.rb,
app/controllers/china_city/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper Classes: ApplicationController, DataController, Engine

Constant Summary collapse

CHINA =

全国

'000000'
PATTERN =
/(\d{2})(\d{2})(\d{2})/
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.city(code) ⇒ Object



46
47
48
49
# File 'lib/china_city.rb', line 46

def city(code)
  id_match = match(code)
  "#{id_match[1]}#{id_match[2]}".ljust(6, '0')
end

.get(id, options = {}) ⇒ Object

@options 是否显示上级区域



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/china_city.rb', line 27

def get(id, options = {})
  return '' if id.blank?
  prepend_parent = options[:prepend_parent] || false
  children = data
  return children[id][:text] if children.has_key?(id)
  province_id = province(id)
  province_text = children[province_id][:text]
  children = children[province_id][:children]
  return "#{prepend_parent ? province_text : ''}#{children[id][:text]}" if children.has_key?(id)
  city_id = city(id)
  city_text = children[city_id][:text]
  children = children[city_id][:children]
  return "#{prepend_parent ? (province_text + city_text) : ''}#{children[id][:text]}"
end

.list(parent_id = '000000') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/china_city.rb', line 9

def list(parent_id = '000000')
  result = []
  return result if parent_id.blank?
  province_id = province(parent_id)
  city_id = city(parent_id)
  children = data
  children = children[province_id][:children] if children.has_key?(province_id)
  children = children[city_id][:children] if children.has_key?(city_id)
  children.each_key do |id|
    result.push [ children[id][:text], id]
  end

  #sort
  result.sort! {|a, b| a[1] <=> b[1]}
  result
end

.province(code) ⇒ Object



42
43
44
# File 'lib/china_city.rb', line 42

def province(code)
  match(code)[1].ljust(6, '0')
end