Class: DistrictCn::Code

Inherits:
Object
  • Object
show all
Includes:
AsOptions
Defined in:
lib/district_cn/code.rb

Constant Summary collapse

REGULAR =
/(\d{2})(\d{2})(\d{2})/

Constants included from AsOptions

AsOptions::OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AsOptions

#as_options

Constructor Details

#initialize(id) ⇒ Code

Returns a new instance of Code.



8
9
10
11
# File 'lib/district_cn/code.rb', line 8

def initialize(id)
  @value = id
  @id = id.to_s
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/district_cn/code.rb', line 6

def id
  @id
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/district_cn/code.rb', line 6

def value
  @value
end

Instance Method Details

#area_name(mark = "-") ⇒ Object



17
18
19
# File 'lib/district_cn/code.rb', line 17

def area_name(mark="-")
  [province_name,city_name,district_name].compact.join(mark) 
end

#childrenObject



21
22
23
24
25
# File 'lib/district_cn/code.rb', line 21

def children
  return [] unless get && get[:children]

  get[:children].map{|id,attr| [attr[:text],id] }
end

#cityObject



52
53
54
# File 'lib/district_cn/code.rb', line 52

def city
  city_id && province && province[:children][city_id]
end

#city?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/district_cn/code.rb', line 39

def city?
  !!(province_id && city_id && district_id.nil?)
end

#city_idObject



79
80
81
82
83
84
# 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_nameObject



64
65
66
# File 'lib/district_cn/code.rb', line 64

def city_name
  city && city[:text]
end

#districtObject



56
57
58
# File 'lib/district_cn/code.rb', line 56

def district
  district_id && city && city[:children][district_id]
end

#district?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/district_cn/code.rb', line 43

def district?
  !!(province_id && city_id && district_id)
end

#district_idObject



86
87
88
89
90
91
# File 'lib/district_cn/code.rb', line 86

def district_id
  @district_id ||= 
    if match && segment_blank?(match[3])
      id
    end
end

#district_nameObject



68
69
70
# File 'lib/district_cn/code.rb', line 68

def district_name
  district && district[:text]
end

#getObject



28
29
30
31
32
33
# File 'lib/district_cn/code.rb', line 28

def get
  @get ||= province? && province
  @get ||= city? && city
  @get ||= district? && district
  @get || nil
end

#nameObject



13
14
15
# File 'lib/district_cn/code.rb', line 13

def name
  get && get[:text]
end

#provinceObject



47
48
49
50
# File 'lib/district_cn/code.rb', line 47

def province
  @province ||=
  province_id && self.class.data && self.class.data[province_id]
end

#province?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/district_cn/code.rb', line 35

def province?
  !!(province_id && city_id.nil?)
end

#province_idObject



72
73
74
75
76
77
# 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_nameObject



60
61
62
# File 'lib/district_cn/code.rb', line 60

def province_name
  province && province[:text]
end