Class: AreaCN::Area
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #children_codes ⇒ Object
- #children_names ⇒ Object
- #city? ⇒ Boolean
- #district? ⇒ Boolean
-
#initialize(area = {}) ⇒ Area
constructor
area is a json area from areas.json file.
- #inspect ⇒ Object
- #parent_code ⇒ Object
- #province? ⇒ Boolean
-
#sense_name ⇒ Object
有很多省(省会城市),市下面有“市辖区”, 单纯的“市辖区”不能分辨是哪个地区的 如果是“市辖区”的地区返回带上级的名字.
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
8 9 10 |
# File 'lib/area_cn/area.rb', line 8 def children @children end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
8 9 10 |
# File 'lib/area_cn/area.rb', line 8 def code @code end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/area_cn/area.rb', line 8 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
9 10 11 |
# File 'lib/area_cn/area.rb', line 9 def parent @parent end |
Instance Method Details
#<=>(other) ⇒ Object
49 50 51 |
# File 'lib/area_cn/area.rb', line 49 def <=>(other) code <=> other.code end |
#children_codes ⇒ Object
33 34 35 |
# File 'lib/area_cn/area.rb', line 33 def children_codes @children_codes ||= children.map(&:code) end |
#children_names ⇒ Object
29 30 31 |
# File 'lib/area_cn/area.rb', line 29 def children_names @children_names ||= children.map(&:name) end |
#city? ⇒ Boolean
41 42 43 |
# File 'lib/area_cn/area.rb', line 41 def city? @code.prefix.length == 4 end |
#district? ⇒ Boolean
45 46 47 |
# File 'lib/area_cn/area.rb', line 45 def district? @code.prefix.length == 6 end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/area_cn/area.rb', line 61 def inspect to_hash.inspect end |
#parent_code ⇒ Object
25 26 27 |
# File 'lib/area_cn/area.rb', line 25 def parent_code @code.parent end |
#province? ⇒ Boolean
37 38 39 |
# File 'lib/area_cn/area.rb', line 37 def province? @code.prefix.length == 2 end |
#sense_name ⇒ Object
有很多省(省会城市),市下面有“市辖区”, 单纯的“市辖区”不能分辨是哪个地区的
如果是“市辖区”的地区返回带上级的名字
21 22 23 |
# File 'lib/area_cn/area.rb', line 21 def sense_name @sense_name ||= name == "市辖区" ? parent.name + "辖区" : name end |
#to_hash ⇒ Object
53 54 55 |
# File 'lib/area_cn/area.rb', line 53 def to_hash {name: name, code: code} end |
#to_json(*args) ⇒ Object
57 58 59 |
# File 'lib/area_cn/area.rb', line 57 def to_json(*args) to_hash.to_json(*args) end |