class DistrictCn::Code

Constants

REGULAR

Attributes

id[R]
value[R]

Public Class Methods

new(id) click to toggle source
# File lib/district_cn/code.rb, line 8
def initialize(id)
  @value = id
  @id = id.to_s
end

Private Class Methods

data() click to toggle source
# File lib/district_cn/code.rb, line 102
def self.data
  @data ||= Db::File.instance.tree
end

Public Instance Methods

area_name(mark="-") click to toggle source
# File lib/district_cn/code.rb, line 17
def area_name(mark="-")
  [province_name,city_name,district_name].compact.join(mark) 
end
children() click to toggle source
# File lib/district_cn/code.rb, line 21
def children
  return [] unless get && get[:children]

  get[:children].map{|id,attr| [attr[:text],id] }
end
city() click to toggle source
# File lib/district_cn/code.rb, line 52
def city
  city_id && province && province[:children][city_id]
end
city?() click to toggle source
# File lib/district_cn/code.rb, line 39
def city?
  !!(province_id && city_id && district_id.nil?)
end
city_id() click to toggle source
# File lib/district_cn/code.rb, line 79
def city_id 
  @city_id ||= 
    if match && segment_blank?(match[2])
      "#{match[1]}#{match[2]}".ljust(6, '0')
    end
end
city_name() click to toggle source
# File lib/district_cn/code.rb, line 64
def city_name
  city && city[:text]
end
district() click to toggle source
# File lib/district_cn/code.rb, line 56
def district
  district_id && city && city[:children][district_id]
end
district?() click to toggle source
# File lib/district_cn/code.rb, line 43
def district?
  !!(province_id && city_id && district_id)
end
district_id() click to toggle source
# File lib/district_cn/code.rb, line 86
def district_id
  @district_id ||= 
    if match && segment_blank?(match[3])
      id
    end
end
district_name() click to toggle source
# File lib/district_cn/code.rb, line 68
def district_name
  district && district[:text]
end
get() click to toggle source
# File lib/district_cn/code.rb, line 28
def get
  @get ||= province? && province
  @get ||= city? && city
  @get ||= district? && district
  @get || nil
end
name() click to toggle source
# File lib/district_cn/code.rb, line 13
def name
  get && get[:text]
end
province() click to toggle source
# File lib/district_cn/code.rb, line 47
def province
  @province ||=
  province_id && self.class.data && self.class.data[province_id]
end
province?() click to toggle source
# File lib/district_cn/code.rb, line 35
def province?
  !!(province_id && city_id.nil?)
end
province_id() click to toggle source
# File lib/district_cn/code.rb, line 72
def province_id
  @province_id ||= 
    if match && segment_blank?(match[1])
      match[1].ljust(6, '0')
    end
end
province_name() click to toggle source
# File lib/district_cn/code.rb, line 60
def province_name
  province && province[:text]
end

Private Instance Methods

match() click to toggle source
# File lib/district_cn/code.rb, line 94
def match 
  @match ||= id.match(REGULAR)
end
segment_blank?(segment) click to toggle source
# File lib/district_cn/code.rb, line 98
def segment_blank?(segment)
  segment != "00"
end