chinese_cities 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,42 +1,47 @@
1
1
  module ChineseCities
2
2
  class Province
3
- attr_accessor :name
4
-
5
- def initialize name
6
- @name = name
3
+ attr_accessor :id, :name
4
+
5
+ def initialize(province)
6
+ @id = province[:id]
7
+ @name = province[:name]
7
8
  end
8
9
 
9
10
  def cities
10
- city_names.map do |city|
11
- City.find city
12
- end
11
+ City.find_by_province_id(id)
13
12
  end
14
13
 
15
14
  def city_names
16
- DATABASE.find do |province|
17
- province.first == name
18
- end.last
15
+ cities.map(&:name)
19
16
  end
20
17
 
21
18
  class << self
22
19
 
23
20
  private :new
24
21
 
25
- def find name
26
- return nil unless all_names.include? name
27
- new name
22
+ def search(name)
23
+ provinces = PROVINCES.select { |province| province[:name] =~ /#{name}/ }
24
+ provinces.map do |province|
25
+ new(province)
26
+ end
27
+ end
28
+
29
+ def find(id)
30
+ province = PROVINCES.find { |province| province[:id] == id }
31
+ new(province) unless province.nil?
32
+ end
33
+
34
+ def where(name)
35
+ province = PROVINCES.find { |province| province[:name] == name }
36
+ new(province) if province
28
37
  end
29
38
 
30
39
  def all_names
31
- DATABASE.map do |province|
32
- province.first
33
- end
40
+ PROVINCES.map { |province| province[:name] }
34
41
  end
35
42
 
36
43
  def all
37
- all_names.map do |province|
38
- new province
39
- end
44
+ PROVINCES.map { |province| new(province) }
40
45
  end
41
46
  end
42
47
 
@@ -0,0 +1,54 @@
1
+ module ChineseCities
2
+ class Region
3
+ attr_accessor :id, :name, :city_id
4
+
5
+ def initialize(region)
6
+ @id = region[:id]
7
+ @city_id = region[:city_id]
8
+ @name = region[:name]
9
+ end
10
+
11
+ def city
12
+ City.find(city_id)
13
+ end
14
+
15
+ def city_name
16
+ city.name
17
+ end
18
+
19
+ class << self
20
+ private :new
21
+
22
+ def search(name)
23
+ regions = REGIONS.select { |region| region[:name] =~ /#{name}/ }
24
+ regions.map do |region|
25
+ new(region)
26
+ end
27
+ end
28
+
29
+ def find_by_city_id(city_id)
30
+ regions = REGIONS.select { |region| region[:city_id] == city_id }
31
+ regions.map { |region| new(region) }
32
+ end
33
+
34
+ def find(id)
35
+ region = REGIONS.find { |region| region[:id] == id }
36
+ new(region) unless region.nil?
37
+ end
38
+
39
+ def where(name)
40
+ regions = REGIONS.select { |region| region[:name] == name }
41
+ regions.map { |region| new(region) } unless regions.empty?
42
+ end
43
+
44
+ def all_names
45
+ REGIONS.map { |region| region[:name] }
46
+ end
47
+
48
+ def all
49
+ REGIONS.map { |region| new(region) }
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
1
  module ChineseCities
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,24 +1,20 @@
1
- # encoding: utf-8
2
-
3
1
  require "chinese_cities/version"
2
+ require "chinese_cities/database"
4
3
 
5
4
  module ChineseCities
6
- autoload :DATABASE, 'chinese_cities/database'
7
5
  autoload :Province, 'chinese_cities/province'
8
- autoload :City, 'chinese_cities/city'
6
+ autoload :City, 'chinese_cities/city'
7
+ autoload :Region, 'chinese_cities/region'
9
8
 
10
- def self.search name
11
- hash = Hash.new
9
+ def self.search(name)
10
+ provinces = Province.search(name)
11
+ cities = City.search(name)
12
+ regions = Region.search(name)
12
13
 
13
- hash[:provinces] = Province.all_names.find_all do |province|
14
- province.include? name
15
- end
16
-
17
- hash[:cities] = City.all_names.find_all do |city|
18
- city.include? name
19
- end
20
-
21
- hash
14
+ {
15
+ provinces: provinces,
16
+ cities: cities,
17
+ regions: regions
18
+ }
22
19
  end
23
-
24
20
  end
@@ -7,7 +7,7 @@ module ChineseCities
7
7
 
8
8
  describe City do
9
9
  before :all do
10
- @city = City.find '西城区'
10
+ @city = City.find(1)
11
11
  end
12
12
 
13
13
  context '#province' do
@@ -24,20 +24,34 @@ module ChineseCities
24
24
  end
25
25
  end
26
26
 
27
+ context '#regions' do
28
+ it 'should have regions' do
29
+ result = @city.regions
30
+ result.should_not be_empty
31
+ end
32
+ end
33
+
34
+ context '#region_name' do
35
+ it 'should have result_names' do
36
+ result = @city.region_names
37
+ result.should_not be_empty
38
+ end
39
+ end
40
+
27
41
  context '.new' do
28
42
  it 'should be a private method' do
29
- lambda { City.new '西城区' }.should raise_error
43
+ lambda { City.new(CITIES.first)}.should raise_error
30
44
  end
31
45
  end
32
46
 
33
47
  context '.find' do
34
48
  it 'should find a city' do
35
- result = City.find '西城区'
49
+ result = City.find(1)
36
50
  result.should_not be_nil
37
51
  end
38
52
 
39
53
  it 'should not find a city' do
40
- result = City.find '啊啊啊'
54
+ result = City.find(0)
41
55
  result.should be_nil
42
56
  end
43
57
  end
@@ -56,5 +70,24 @@ module ChineseCities
56
70
  end
57
71
  end
58
72
 
73
+ context '.where' do
74
+ it 'should get some cities' do
75
+ result = City.where('北京市')
76
+ result.should_not be_empty
77
+ end
78
+
79
+ it 'should not get city' do
80
+ result = City.where('哈哈')
81
+ result.should be_nil
82
+ end
83
+ end
84
+
85
+ context '.find_by_province_id' do
86
+ it 'should find by province_id' do
87
+ result = City.find_by_province_id(1)
88
+ result.should_not be_empty
89
+ end
90
+ end
91
+
59
92
  end
60
93
  end
@@ -7,7 +7,7 @@ module ChineseCities
7
7
 
8
8
  describe Province do
9
9
  before :all do
10
- @province = Province.find '北京市'
10
+ @province = Province.find(1)
11
11
  end
12
12
 
13
13
  context '#cities' do
@@ -26,18 +26,18 @@ module ChineseCities
26
26
 
27
27
  context '.new' do
28
28
  it 'should be a private method' do
29
- lambda { Province.new '北京市' }.should raise_error
29
+ lambda { Province.new(PROVINCES.first) }.should raise_error
30
30
  end
31
31
  end
32
32
 
33
33
  context '.find' do
34
34
  it 'should find a province' do
35
- result = Province.find '天津市'
35
+ result = Province.find(1)
36
36
  result.should_not be_nil
37
37
  end
38
38
 
39
39
  it 'should not find a province' do
40
- result = Province.find '啊啊啊'
40
+ result = Province.find(0)
41
41
  result.should be_nil
42
42
  end
43
43
  end
@@ -56,5 +56,17 @@ module ChineseCities
56
56
  end
57
57
  end
58
58
 
59
+ context '.where' do
60
+ it 'should get a province' do
61
+ result = Province.where('北京市')
62
+ result.should_not nil
63
+ end
64
+
65
+ it 'should not get any province' do
66
+ result = Province.where('哈哈')
67
+ result.should nil
68
+ end
69
+ end
70
+
59
71
  end
60
72
  end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rspec'
4
+ require 'chinese_cities/region'
5
+
6
+ module ChineseCities
7
+ describe Region do
8
+ before :all do
9
+ @region = Region.find(1)
10
+ end
11
+
12
+ context '#city' do
13
+ it 'should have city' do
14
+ result = @region.city
15
+ result.should_not be_nil
16
+ end
17
+ end
18
+
19
+ context '#city_name' do
20
+ it 'should have city_name' do
21
+ result = @region.city_name
22
+ result.should_not be_nil
23
+ end
24
+ end
25
+
26
+ context '.new' do
27
+ it 'should be a private method' do
28
+ lambda { Region.new(REGIONS)}.should raise_error
29
+ end
30
+ end
31
+
32
+ context '.find' do
33
+ it 'should find a region' do
34
+ result = Region.find(1)
35
+ result.should_not be_nil
36
+ end
37
+
38
+ it 'should not find a region' do
39
+ result = Region.find(0)
40
+ result.should be_nil
41
+ end
42
+ end
43
+
44
+ context '.all' do
45
+ it 'should get all region' do
46
+ result = Region.all
47
+ result.should_not be_empty
48
+ end
49
+ end
50
+
51
+ context '.all_names' do
52
+ it 'should get all region names' do
53
+ result = Region.all_names
54
+ result.should_not be_empty
55
+ end
56
+ end
57
+
58
+ context '.where' do
59
+ it 'should get some region' do
60
+ result = Region.where('东城区')
61
+ result.should_not be_empty
62
+ end
63
+
64
+ it 'should not get region' do
65
+ result = Region.where('哈哈')
66
+ result.should be_nil
67
+ end
68
+ end
69
+
70
+ context '.find_by_city_id' do
71
+ it 'should find by city_id' do
72
+ result = Region.find_by_city_id(1)
73
+ result.should_not be_empty
74
+ end
75
+ end
76
+ end
77
+ end
78
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chinese_cities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-10 00:00:00.000000000 Z
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -44,9 +44,11 @@ files:
44
44
  - lib/chinese_cities/city.rb
45
45
  - lib/chinese_cities/database.rb
46
46
  - lib/chinese_cities/province.rb
47
+ - lib/chinese_cities/region.rb
47
48
  - lib/chinese_cities/version.rb
48
49
  - spec/chinese_cities/city_spec.rb
49
50
  - spec/chinese_cities/province_spec.rb
51
+ - spec/chinese_cities/region_spec.rb
50
52
  - spec/chinese_cities_spec.rb
51
53
  homepage: https://github.com/zhouguangming/chinese_cities
52
54
  licenses: []
@@ -68,11 +70,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
70
  version: '0'
69
71
  requirements: []
70
72
  rubyforge_project:
71
- rubygems_version: 1.8.24
73
+ rubygems_version: 1.8.25
72
74
  signing_key:
73
75
  specification_version: 3
74
76
  summary: Chinese provinces and cities collection
75
77
  test_files:
76
78
  - spec/chinese_cities/city_spec.rb
77
79
  - spec/chinese_cities/province_spec.rb
80
+ - spec/chinese_cities/region_spec.rb
78
81
  - spec/chinese_cities_spec.rb