class DistrictCn::Db::File
Attributes
list[R]
tree[R]
Public Class Methods
instance()
click to toggle source
# File lib/district_cn/db.rb, line 18 def instance @instance ||= new end
list()
click to toggle source
# File lib/district_cn/db.rb, line 30 def list instance.list end
provinces()
click to toggle source
# File lib/district_cn/db.rb, line 22 def provinces instance.provinces end
tree()
click to toggle source
# File lib/district_cn/db.rb, line 26 def tree instance.tree end
Private Class Methods
new()
click to toggle source
# File lib/district_cn/db.rb, line 11 def initialize @tree = {} @list = {} parse end
Public Instance Methods
provinces()
click to toggle source
# File lib/district_cn/db.rb, line 7 def provinces @provinces ||= @tree.map{|pvn_id,pvn_hash| [pvn_hash[:text],pvn_id]} end
Private Instance Methods
code_regular()
click to toggle source
# File lib/district_cn/db.rb, line 38 def code_regular DistrictCn::Code::REGULAR end
json_data()
click to toggle source
# File lib/district_cn/db.rb, line 46 def json_data @json_data ||= JSON.parse(::File.read(path)) end
parse()
click to toggle source
# File lib/district_cn/db.rb, line 50 def parse json_data["province"].each do |province| @tree[province["id"]] = {:text => province["text"],:children => {}} @list[province["id"]] = province["text"] end json_data["city"].each do |city| city["id"] =~ (code_regular) province_id = $1.ljust(6, '0') if @tree[province_id] @tree[province_id][:children][city["id"]] = {:text => city["text"], :children => {}} @list[city["id"]] = city["text"] end end json_data["district"].each do |district| district["id"] =~ (code_regular) province_id = $1.ljust(6, '0') city_id = "#{$1}#{$2}".ljust(6, '0') if @tree[province_id] && @tree[province_id][:children][city_id] @tree[province_id][:children][city_id][:children][district["id"]] = {:text => district["text"]} @list[district["id"]] = district["text"] end end end
path()
click to toggle source
# File lib/district_cn/db.rb, line 42 def path ::File.expand_path("../../areas.json", __FILE__) end