Class: DistrictCn::Code
- Inherits:
-
Object
- Object
- DistrictCn::Code
- Includes:
- AsOptions
- Defined in:
- lib/district_cn/code.rb
Constant Summary collapse
- REGULAR =
/(\d{2})(\d{2})(\d{2})/
Constants included from AsOptions
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #area_name(mark = "-") ⇒ Object
- #children ⇒ Object
- #city ⇒ Object
- #city? ⇒ Boolean
- #city_id ⇒ Object
- #city_name ⇒ Object
- #district ⇒ Object
- #district? ⇒ Boolean
- #district_id ⇒ Object
- #district_name ⇒ Object
- #get ⇒ Object
-
#initialize(id) ⇒ Code
constructor
A new instance of Code.
- #name ⇒ Object
- #province ⇒ Object
- #province? ⇒ Boolean
- #province_id ⇒ Object
- #province_name ⇒ Object
Methods included from AsOptions
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/district_cn/code.rb', line 6 def id @id end |
#value ⇒ Object (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 |
#children ⇒ Object
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 |
#city ⇒ Object
52 53 54 |
# File 'lib/district_cn/code.rb', line 52 def city city_id && province && province[:children][city_id] end |
#city? ⇒ Boolean
39 40 41 |
# File 'lib/district_cn/code.rb', line 39 def city? !!(province_id && city_id && district_id.nil?) end |
#city_id ⇒ Object
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_name ⇒ Object
64 65 66 |
# File 'lib/district_cn/code.rb', line 64 def city_name city && city[:text] end |
#district ⇒ Object
56 57 58 |
# File 'lib/district_cn/code.rb', line 56 def district district_id && city && city[:children][district_id] end |
#district? ⇒ Boolean
43 44 45 |
# File 'lib/district_cn/code.rb', line 43 def district? !!(province_id && city_id && district_id) end |
#district_id ⇒ Object
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_name ⇒ Object
68 69 70 |
# File 'lib/district_cn/code.rb', line 68 def district_name district && district[:text] end |
#get ⇒ Object
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 |
#name ⇒ Object
13 14 15 |
# File 'lib/district_cn/code.rb', line 13 def name get && get[:text] end |
#province ⇒ Object
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
35 36 37 |
# File 'lib/district_cn/code.rb', line 35 def province? !!(province_id && city_id.nil?) end |
#province_id ⇒ Object
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_name ⇒ Object
60 61 62 |
# File 'lib/district_cn/code.rb', line 60 def province_name province && province[:text] end |