i18n-sequel_bitemporal 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/ci/Gemfile.rails-3.x CHANGED
@@ -7,3 +7,4 @@ gem 'sqlite3-ruby'
7
7
  gem 'mocha'
8
8
  gem 'test_declarative'
9
9
  gem 'rake'
10
+ gem 'timecop'
@@ -7,3 +7,4 @@ gem 'sqlite3-ruby'
7
7
  gem 'mocha'
8
8
  gem 'test_declarative'
9
9
  gem 'rake'
10
+ gem 'timecop'
@@ -3,16 +3,17 @@ GEM
3
3
  specs:
4
4
  i18n (0.6.0)
5
5
  metaclass (0.0.1)
6
- mocha (0.10.0)
6
+ mocha (0.11.3)
7
7
  metaclass (~> 0.0.1)
8
8
  rake (0.9.2.2)
9
9
  sequel (3.30.0)
10
- sequel_bitemporal (0.4.1)
10
+ sequel_bitemporal (0.4.9)
11
11
  sequel (~> 3.30.0)
12
- sqlite3 (1.3.4)
12
+ sqlite3 (1.3.6)
13
13
  sqlite3-ruby (1.3.3)
14
14
  sqlite3 (>= 1.3.3)
15
15
  test_declarative (0.0.5)
16
+ timecop (0.3.5)
16
17
 
17
18
  PLATFORMS
18
19
  ruby
@@ -24,3 +25,4 @@ DEPENDENCIES
24
25
  sequel_bitemporal (~> 0.4.0)
25
26
  sqlite3-ruby
26
27
  test_declarative
28
+ timecop
@@ -2,14 +2,18 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  i18n (0.5.0)
5
- mocha (0.9.9)
6
- rake
7
- rake (0.8.7)
5
+ metaclass (0.0.1)
6
+ mocha (0.11.3)
7
+ metaclass (~> 0.0.1)
8
+ rake (0.9.2.2)
8
9
  sequel (3.30.0)
9
- sequel_bitemporal (0.4.0)
10
+ sequel_bitemporal (0.4.9)
10
11
  sequel (~> 3.30.0)
11
- sqlite3-ruby (1.3.1)
12
+ sqlite3 (1.3.6)
13
+ sqlite3-ruby (1.3.3)
14
+ sqlite3 (>= 1.3.3)
12
15
  test_declarative (0.0.5)
16
+ timecop (0.3.5)
13
17
 
14
18
  PLATFORMS
15
19
  ruby
@@ -21,3 +25,4 @@ DEPENDENCIES
21
25
  sequel_bitemporal (~> 0.4.0)
22
26
  sqlite3-ruby
23
27
  test_declarative
28
+ timecop
@@ -26,4 +26,5 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency "mocha"
27
27
  s.add_development_dependency "rake"
28
28
  s.add_development_dependency "sqlite3"
29
+ s.add_development_dependency "timecop"
29
30
  end
@@ -140,6 +140,10 @@ module I18n
140
140
  def available_locales
141
141
  Translation.distinct.select(:locale).map { |t| t.locale.to_sym }
142
142
  end
143
+
144
+ def all_for_locale(locale)
145
+ self.locale(locale).with_current_version.all
146
+ end
143
147
  end
144
148
  end
145
149
  end
@@ -13,6 +13,7 @@ module I18n
13
13
  def initialize(opts= {})
14
14
  @preload_all = opts.fetch(:preload_all, false)
15
15
  @translations = {}
16
+ @cache_times = {}
16
17
  end
17
18
 
18
19
  def available_locales
@@ -40,10 +41,13 @@ module I18n
40
41
  end
41
42
  end
42
43
 
43
- def clear(locale=:all)
44
+ def clear(locale=:all, options={})
44
45
  if locale==:all
45
- @translations = {}
46
+ @translations.keys.each{|loc| clear loc, options }
46
47
  else
48
+ last_update = options[:last_update]
49
+ cache_time = @cache_times[locale]
50
+ return if last_update && cache_time && last_update < cache_time
47
51
  @translations[locale] = nil
48
52
  end
49
53
  end
@@ -52,7 +56,10 @@ module I18n
52
56
 
53
57
  def fetch_all_translations(locale)
54
58
  @translations ||= {}
55
- @translations[locale] ||= Translation.locale(locale).with_current_version.all
59
+ @translations[locale] ||= begin
60
+ @cache_times[locale] = Time.now
61
+ Translation.all_for_locale(locale)
62
+ end
56
63
  @translations[locale]
57
64
  end
58
65
 
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module SequelBitemporal
3
- VERSION = "0.4.2"
3
+ VERSION = "0.4.3"
4
4
  end
5
5
  end
@@ -0,0 +1,141 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class I18nBackendSequelBitemporalCacheTest < Test::Unit::TestCase
4
+ def clear_all
5
+ I18n::Backend::SequelBitemporal::Translation.delete
6
+ end
7
+
8
+ def setup
9
+ I18n.backend = I18n::Backend::SequelBitemporal.new :preload_all => true
10
+ store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
11
+ store_translations(:fr, :foo => { :bar => 'bar', :baz => 'baz' })
12
+ end
13
+
14
+ def teardown
15
+ clear_all
16
+ super
17
+ end
18
+
19
+ with_mocha do
20
+ test "cache all translation for current locales" do
21
+ translations_en = I18n::Backend::SequelBitemporal::Translation.locale(:en).lookup('foo').all
22
+ translations_fr = I18n::Backend::SequelBitemporal::Translation.locale(:fr).lookup('foo').all
23
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
24
+ with(:en).
25
+ returns(translations_en)
26
+ I18n.t(:foo)
27
+ I18n.t(:foo)
28
+
29
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
30
+ with(:fr).
31
+ returns(translations_fr)
32
+ I18n.with_locale :fr do
33
+ I18n.t(:foo)
34
+ I18n.t(:foo)
35
+ end
36
+ end
37
+
38
+ test "clear all locales" do
39
+ translations_en = translations_for :en
40
+ translations_fr = translations_for :fr
41
+
42
+ I18n.t(:foo)
43
+ I18n.with_locale(:fr) { I18n.t(:foo) }
44
+
45
+ I18n.backend.clear
46
+
47
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
48
+ with(:en).
49
+ returns(translations_en)
50
+ I18n.t(:foo) # load from db
51
+
52
+ I18n.with_locale :fr do
53
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
54
+ with(:fr).
55
+ returns(translations_fr)
56
+ I18n.t(:foo) # load from db
57
+ end
58
+ end
59
+
60
+ test "clear specified locale only" do
61
+ translations_en = translations_for :en
62
+ translations_fr = translations_for :fr
63
+
64
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
65
+ with(:en).returns(translations_en)
66
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
67
+ with(:fr).returns(translations_fr)
68
+ I18n.t(:foo)
69
+ I18n.with_locale(:fr) { I18n.t(:foo) }
70
+
71
+ I18n.backend.clear :en
72
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
73
+ with(:en).returns(translations_en)
74
+ I18n.t(:foo)
75
+ I18n.with_locale(:fr) { I18n.t(:foo) }
76
+ end
77
+
78
+ test "clear all locales with stale cache" do
79
+ translations_en = translations_for :en
80
+ translations_fr = translations_for :fr
81
+
82
+ last_update = Time.now
83
+ en_cache_time = last_update-60
84
+ fr_cache_time = last_update-10
85
+
86
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
87
+ with(:en).returns(translations_en)
88
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
89
+ with(:fr).returns(translations_fr)
90
+
91
+ Timecop.freeze(en_cache_time){ I18n.t(:foo) }
92
+ Timecop.freeze(fr_cache_time) do
93
+ I18n.with_locale(:fr) { I18n.t(:foo) }
94
+ end
95
+
96
+ I18n.backend.clear :all, :last_update => last_update-65
97
+ I18n.t(:foo)
98
+ I18n.with_locale(:fr) { I18n.t(:foo) }
99
+
100
+ I18n.backend.clear :all, :last_update => last_update-15
101
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
102
+ with(:en).returns(translations_en)
103
+ I18n.t(:foo)
104
+ I18n.with_locale(:fr) { I18n.t(:foo) }
105
+ end
106
+
107
+ test "clear specific locale with stale cache" do
108
+ translations_en = translations_for :en
109
+ translations_fr = translations_for :fr
110
+
111
+ last_update = Time.now
112
+ en_cache_time = last_update-60
113
+ fr_cache_time = last_update-10
114
+
115
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
116
+ with(:en).returns(translations_en)
117
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
118
+ with(:fr).returns(translations_fr)
119
+
120
+ Timecop.freeze(en_cache_time){ I18n.t(:foo) }
121
+ Timecop.freeze(fr_cache_time) do
122
+ I18n.with_locale(:fr) { I18n.t(:foo) }
123
+ end
124
+
125
+ I18n.backend.clear :fr, :last_update => last_update-65
126
+ I18n.t(:foo)
127
+ I18n.with_locale(:fr) { I18n.t(:foo) }
128
+
129
+ I18n.backend.clear :fr, :last_update => last_update
130
+ I18n::Backend::SequelBitemporal::Translation.expects(:all_for_locale).
131
+ with(:fr).returns(translations_en)
132
+ I18n.t(:foo)
133
+ I18n.with_locale(:fr) { I18n.t(:foo) }
134
+ end
135
+ end
136
+
137
+ def translations_for(locale)
138
+ I18n::Backend::SequelBitemporal::Translation.locale(:locale).all
139
+ end
140
+
141
+ end if defined?(Sequel)
data/test/sequel_test.rb CHANGED
@@ -14,7 +14,7 @@ class I18nBackendSequelBitemporalTest < Test::Unit::TestCase
14
14
  clear_all
15
15
  super
16
16
  end
17
-
17
+
18
18
  test "store_translations does not allow ambiguous keys (1)" do
19
19
  clear_all
20
20
  I18n.backend.store_translations(:en, :foo => 'foo')
@@ -57,7 +57,8 @@ class I18nBackendSequelBitemporalTest < Test::Unit::TestCase
57
57
  end
58
58
  end
59
59
 
60
- def test_expand_keys
60
+ test "expand keys" do
61
61
  assert_equal %w(foo foo.bar foo.bar.baz), I18n.backend.send(:expand_keys, :'foo.bar.baz')
62
62
  end
63
- end if defined?(Sequel)
63
+
64
+ end if defined?(Sequel)
data/test/test_helper.rb CHANGED
@@ -9,6 +9,7 @@ require 'i18n/sequel_bitemporal'
9
9
  require 'i18n/tests'
10
10
  require 'mocha'
11
11
  require 'test_declarative'
12
+ require 'timecop'
12
13
  I18n::Tests.setup_sequel
13
14
 
14
15
  class Test::Unit::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-sequel_bitemporal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-29 00:00:00.000000000 Z
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &2151823340 !ruby/object:Gem::Requirement
16
+ requirement: &2166051280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: 0.7.0
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *2151823340
27
+ version_requirements: *2166051280
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: sequel_bitemporal
30
- requirement: &2151821980 !ruby/object:Gem::Requirement
30
+ requirement: &2166050220 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ~>
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: 0.4.0
36
36
  type: :runtime
37
37
  prerelease: false
38
- version_requirements: *2151821980
38
+ version_requirements: *2166050220
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: test_declarative
41
- requirement: &2151820720 !ruby/object:Gem::Requirement
41
+ requirement: &2166049420 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ! '>='
@@ -46,10 +46,10 @@ dependencies:
46
46
  version: '0'
47
47
  type: :development
48
48
  prerelease: false
49
- version_requirements: *2151820720
49
+ version_requirements: *2166049420
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: mocha
52
- requirement: &2151819340 !ruby/object:Gem::Requirement
52
+ requirement: &2166048120 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
55
  - - ! '>='
@@ -57,10 +57,10 @@ dependencies:
57
57
  version: '0'
58
58
  type: :development
59
59
  prerelease: false
60
- version_requirements: *2151819340
60
+ version_requirements: *2166048120
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rake
63
- requirement: &2151818520 !ruby/object:Gem::Requirement
63
+ requirement: &2166057160 !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
66
66
  - - ! '>='
@@ -68,10 +68,10 @@ dependencies:
68
68
  version: '0'
69
69
  type: :development
70
70
  prerelease: false
71
- version_requirements: *2151818520
71
+ version_requirements: *2166057160
72
72
  - !ruby/object:Gem::Dependency
73
73
  name: sqlite3
74
- requirement: &2151812440 !ruby/object:Gem::Requirement
74
+ requirement: &2166072120 !ruby/object:Gem::Requirement
75
75
  none: false
76
76
  requirements:
77
77
  - - ! '>='
@@ -79,7 +79,18 @@ dependencies:
79
79
  version: '0'
80
80
  type: :development
81
81
  prerelease: false
82
- version_requirements: *2151812440
82
+ version_requirements: *2166072120
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: &2166070140 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: *2166070140
83
94
  description: I18n Bitemporal Sequel backend. Allows to store translations in a database
84
95
  using Sequel using a bitemporal approach, e.g. for providing a web-interface for
85
96
  managing translations.
@@ -109,6 +120,7 @@ files:
109
120
  - test/all.rb
110
121
  - test/api_test.rb
111
122
  - test/missing_test.rb
123
+ - test/sequel_cache_test.rb
112
124
  - test/sequel_test.rb
113
125
  - test/test_helper.rb
114
126
  - test/test_setup.rb
@@ -126,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
138
  version: '0'
127
139
  segments:
128
140
  - 0
129
- hash: 3584568548575898689
141
+ hash: 523352009748703952
130
142
  required_rubygems_version: !ruby/object:Gem::Requirement
131
143
  none: false
132
144
  requirements:
@@ -135,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
147
  version: '0'
136
148
  segments:
137
149
  - 0
138
- hash: 3584568548575898689
150
+ hash: 523352009748703952
139
151
  requirements: []
140
152
  rubyforge_project: ! '[none]'
141
153
  rubygems_version: 1.8.10
@@ -146,6 +158,7 @@ test_files:
146
158
  - test/all.rb
147
159
  - test/api_test.rb
148
160
  - test/missing_test.rb
161
+ - test/sequel_cache_test.rb
149
162
  - test/sequel_test.rb
150
163
  - test/test_helper.rb
151
164
  - test/test_setup.rb