Class: AreaCN::Areas
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
-
#cities ⇒ Object
readonly
Returns the value of attribute cities.
-
#districts ⇒ Object
readonly
Returns the value of attribute districts.
-
#provinces ⇒ Object
readonly
Returns the value of attribute provinces.
Instance Method Summary collapse
- #find_all_by_name(name, area_level = nil) ⇒ Object
- #find_by_code(code, area_level = nil) ⇒ Object (also: #get)
- #find_by_name(name, area_level = nil) ⇒ Object
-
#initialize ⇒ Areas
constructor
A new instance of Areas.
- #match(name, area_level = nil) ⇒ Object
Constructor Details
#initialize ⇒ Areas
Returns a new instance of Areas.
10 11 12 13 14 15 16 17 18 |
# File 'lib/area_cn/areas.rb', line 10 def initialize @provinces = json_data['province'].map { |area_attr| Province.new(area_attr) } @cities = json_data['city'].map { |area_attr| City.new(area_attr) } @districts = json_data['district'].map { |area_attr| District.new(area_attr) } @all = @provinces + @cities + @districts locate_areas end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
8 9 10 |
# File 'lib/area_cn/areas.rb', line 8 def all @all end |
#cities ⇒ Object (readonly)
Returns the value of attribute cities.
8 9 10 |
# File 'lib/area_cn/areas.rb', line 8 def cities @cities end |
#districts ⇒ Object (readonly)
Returns the value of attribute districts.
8 9 10 |
# File 'lib/area_cn/areas.rb', line 8 def districts @districts end |
#provinces ⇒ Object (readonly)
Returns the value of attribute provinces.
8 9 10 |
# File 'lib/area_cn/areas.rb', line 8 def provinces @provinces end |
Instance Method Details
#find_all_by_name(name, area_level = nil) ⇒ Object
20 21 22 23 |
# File 'lib/area_cn/areas.rb', line 20 def find_all_by_name(name, area_level = nil) scope = area_level ? instance_variable_get("@#{area_level.to_s.pluralize}") : all scope.select { |area| area.name == name } end |
#find_by_code(code, area_level = nil) ⇒ Object Also known as: get
30 31 32 33 |
# File 'lib/area_cn/areas.rb', line 30 def find_by_code(code, area_level = nil) scope = area_level ? instance_variable_get("@#{area_level.to_s.pluralize}") : all scope.detect { |area| area.code == code } end |
#find_by_name(name, area_level = nil) ⇒ Object
25 26 27 28 |
# File 'lib/area_cn/areas.rb', line 25 def find_by_name(name, area_level = nil) scope = area_level ? instance_variable_get("@#{area_level.to_s.pluralize}") : all scope.detect { |area| area.name == name } end |
#match(name, area_level = nil) ⇒ Object
36 37 38 39 |
# File 'lib/area_cn/areas.rb', line 36 def match(name, area_level = nil) scope = area_level ? instance_variable_get("@#{area_level.to_s.pluralize}") : all scope.select { |area| area.name =~ /^#{name}/ } end |