zipcode_ja 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module ZipcodeJa
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/zipcode_ja.rb CHANGED
@@ -4,21 +4,34 @@ require "csv"
4
4
 
5
5
  module ZipcodeJa
6
6
  @_zipcodes = {}
7
+ @_cache_length = 20
7
8
 
8
9
  class << self
9
10
  def find(zipcode)
10
- return nil unless zipcode =~ /^[0-9]{7}$/
11
+ zipcode =~ /^[0-9]{7}$/ or return
11
12
 
12
13
  cc = zipcode[0, 2].to_sym
13
14
  if !@_zipcodes[cc]
15
+ zip_path = File.join(File.dirname(__FILE__), "../data/zipcode-#{cc}xxxxx.csv")
16
+ FileTest.file?(zip_path) or return
17
+
14
18
  @_zipcodes[cc] = {}
15
- zip_path = File.open(File.join(File.dirname(__FILE__), "../data/zipcode-#{cc}xxxxx.csv"))
16
- CSV.foreach(zip_path) do |row|
19
+ CSV.foreach(File.open(zip_path)) do |row|
17
20
  @_zipcodes[cc][row[2]] = row
18
21
  end
22
+
23
+ clear_cache if @_zipcodes.length > @_cache_length
19
24
  end
20
25
 
21
26
  @_zipcodes[cc][zipcode]
22
27
  end
28
+
29
+ private
30
+
31
+ def clear_cache
32
+ cc = @_zipcodes.first[0]
33
+ @_zipcodes[cc].clear
34
+ @_zipcodes.delete(cc)
35
+ end
23
36
  end
24
37
  end
@@ -23,6 +23,18 @@ describe ZipcodeJa do
23
23
  end
24
24
 
25
25
  it 'notfound region from zipcode' do
26
- expect('986074'.to_region).to eq(nil)
26
+ expect('9999999'.to_region).to eq(nil)
27
+ end
28
+
29
+ it do
30
+ ZipcodeJa.instance_eval { @_cache_length = 5 }
31
+ expect {
32
+ 10.times do |i|
33
+ ("%0d" % i + '00000').to_region
34
+ end
35
+ 10.times do |i|
36
+ ("%0d" % i + '00001').to_region
37
+ end
38
+ }.to_not raise_error
27
39
  end
28
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipcode_ja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
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: 2013-06-07 00:00:00.000000000 Z
12
+ date: 2013-06-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ZipcodeJa allows you to convert Japanese zipcode into a region.
15
15
  email: