tr8n_core 4.0.7 → 4.0.9

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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/lib/tr8n/api_client.rb +28 -18
  4. data/lib/tr8n/application.rb +37 -64
  5. data/lib/tr8n/cache.rb +8 -41
  6. data/lib/tr8n/cache_adapters/file.rb +1 -2
  7. data/lib/tr8n/cache_adapters/memcache.rb +2 -2
  8. data/lib/tr8n/cache_adapters/redis.rb +2 -2
  9. data/lib/tr8n/config.rb +48 -13
  10. data/lib/tr8n/decorators/html.rb +3 -10
  11. data/lib/tr8n/language.rb +25 -112
  12. data/lib/tr8n/language_case.rb +1 -1
  13. data/lib/tr8n/session.rb +2 -9
  14. data/lib/tr8n/source.rb +12 -44
  15. data/lib/tr8n/{tokens/data_tokenizer.rb → tokenizers/data.rb} +4 -3
  16. data/lib/tr8n/{tokens/decoration_tokenizer.rb → tokenizers/decoration.rb} +6 -5
  17. data/lib/tr8n/tokenizers/dom.rb +346 -0
  18. data/lib/tr8n/tokens/data.rb +15 -22
  19. data/lib/tr8n/tokens/transform.rb +24 -12
  20. data/lib/tr8n/translation_key.rb +6 -29
  21. data/lib/tr8n/utils.rb +5 -0
  22. data/lib/tr8n_core/version.rb +1 -1
  23. data/lib/tr8n_core.rb +7 -26
  24. metadata +30 -100
  25. data/lib/tr8n/cache_adapters/cdb.rb +0 -87
  26. data/lib/tr8n/tokens/hidden.rb +0 -57
  27. data/spec/application_spec.rb +0 -52
  28. data/spec/base_spec.rb +0 -19
  29. data/spec/cache/adapters/file_spec.rb +0 -32
  30. data/spec/cache/adapters/memcache_spec.rb +0 -15
  31. data/spec/cache/generators/file_generator_spec.rb +0 -30
  32. data/spec/config_spec.rb +0 -32
  33. data/spec/decorator_spec.rb +0 -12
  34. data/spec/decorators/base_spec.rb +0 -14
  35. data/spec/decorators/default_spec.rb +0 -12
  36. data/spec/decorators/html_spec.rb +0 -50
  37. data/spec/ext/array_spec.rb +0 -12
  38. data/spec/ext/hash_spec.rb +0 -15
  39. data/spec/ext/string_spec.rb +0 -10
  40. data/spec/fixtures/application.json +0 -112
  41. data/spec/fixtures/languages/en-US.json +0 -1424
  42. data/spec/fixtures/languages/es.json +0 -291
  43. data/spec/fixtures/languages/ru.json +0 -582
  44. data/spec/fixtures/translations/ru/basic.json +0 -56
  45. data/spec/fixtures/translations/ru/counters.json +0 -43
  46. data/spec/fixtures/translations/ru/genders.json +0 -171
  47. data/spec/fixtures/translations/ru/last_names.txt +0 -200
  48. data/spec/fixtures/translations/ru/names.json +0 -1
  49. data/spec/fixtures/translations/ru/names.txt +0 -458
  50. data/spec/language_case_rule_spec.rb +0 -57
  51. data/spec/language_case_spec.rb +0 -58
  52. data/spec/language_context_rule_spec.rb +0 -75
  53. data/spec/language_context_spec.rb +0 -333
  54. data/spec/language_spec.rb +0 -615
  55. data/spec/logger_spec.rb +0 -15
  56. data/spec/rules_engine/evaluator_spec.rb +0 -150
  57. data/spec/rules_engine/parser_spec.rb +0 -31
  58. data/spec/source_spec.rb +0 -13
  59. data/spec/spec_helper.rb +0 -101
  60. data/spec/tokens/data_spec.rb +0 -114
  61. data/spec/tokens/data_tokenizer_spec.rb +0 -29
  62. data/spec/tokens/decoration_tokenizer_spec.rb +0 -81
  63. data/spec/tokens/hidden_spec.rb +0 -24
  64. data/spec/tokens/method_spec.rb +0 -84
  65. data/spec/tokens/transform_spec.rb +0 -50
  66. data/spec/translation_key_spec.rb +0 -96
  67. data/spec/translation_spec.rb +0 -24
  68. data/spec/utils_spec.rb +0 -62
@@ -1,333 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Tr8n::LanguageContext do
6
-
7
- before :all do
8
- @app = init_application
9
- @english = @app.language('en-US')
10
- @russian = @app.language('ru')
11
- end
12
-
13
- describe "initialize" do
14
- it "sets attributes" do
15
- expect(Tr8n::LanguageContext.attributes).to eq([:language, :keyword, :description, :default_key, :token_expression, :variables, :token_mapping, :keys, :rules])
16
- end
17
- end
18
-
19
- describe "finding context" do
20
- it "should return correct context" do
21
- #@user = stub_object(:first_name => "Mike", :gender => "male")
22
- #@translator = stub_object(:name => "Mike", :user => @user, :gender => "male")
23
-
24
- expect(@russian.context_by_keyword("date").keyword).to eq("date")
25
- end
26
- end
27
-
28
- describe "matching tokens" do
29
- it "should identify the right tokens" do
30
- @context = Tr8n::LanguageContext.new(
31
- :language => @english,
32
- :keyword => "numeric",
33
- :token_expression => '/.*(count|num|age|hours|minutes|years|seconds)(\d)*$/',
34
- :variables => ['@n'],
35
- :description => "Numeric language context"
36
- )
37
-
38
- expect(@context.token_expression).to eq(/.*(count|num|age|hours|minutes|years|seconds)(\d)*$/)
39
- expect(@context.applies_to_token?("num")).to be_truthy
40
- expect(@context.applies_to_token?("num1")).to be_truthy
41
- expect(@context.applies_to_token?("profile_num1")).to be_truthy
42
- expect(@context.applies_to_token?("profile_num21")).to be_truthy
43
- expect(@context.applies_to_token?("profile")).to be_falsey
44
- expect(@context.applies_to_token?("num_years")).to be_truthy
45
- expect(@context.applies_to_token?("count1")).to be_truthy
46
- expect(@context.applies_to_token?("count2")).to be_truthy
47
- end
48
- end
49
-
50
- describe "gender rules" do
51
- describe "loading variables" do
52
- it "should return assigned variables" do
53
- context = Tr8n::LanguageContext.new(
54
- :language => @english,
55
- :keyword => "gender",
56
- :token_expression => '/.*(profile|user|actor|target)(\d)*$/',
57
- :variables => ['@gender'],
58
- :description => "Gender language context"
59
- )
60
-
61
- obj = double(:gender => "male")
62
- Tr8n.config.stub(:context_rules) {
63
- {
64
- "gender" => {
65
- "variables" => {
66
- "@gender" => "gender",
67
- }
68
- }
69
- }
70
- }
71
- expect(context.vars(obj)).to eq({"@gender" => "male"})
72
-
73
- Tr8n.config.stub(:context_rules) {
74
- {
75
- :gender => {
76
- :variables => {
77
- "@gender" => "gender",
78
- }
79
- }
80
- }
81
- }
82
- expect(context.vars(obj)).to eq({"@gender" => "male"})
83
- expect(context.vars({:object => obj})).to eq({"@gender" => "male"})
84
- expect(context.vars({:gender => "male"})).to eq({"@gender" => "male"})
85
- expect(context.vars(:object => {:gender => "male"})).to eq({"@gender" => "male"})
86
-
87
- Tr8n.config.stub(:context_rules) {
88
- {
89
- "gender" => {
90
- "variables" => {
91
- "@gender" => lambda{|obj| obj.gender},
92
- }
93
- }
94
- }
95
- }
96
- expect(context.vars(obj)).to eq({"@gender" => "male"})
97
- end
98
- end
99
-
100
- describe "evaluate rules" do
101
- it "should return matching rule" do
102
- context = Tr8n::LanguageContext.new(
103
- :language => @english,
104
- :keyword => "gender",
105
- :token_expression => '/.*(profile|user|actor|target)(\d)*$/',
106
- :variables => ['@gender'],
107
- :description => "Gender language context",
108
- :rules => {
109
- :male => {:language_context => @context, :keyword => "male", :conditions => "(= 'male' @gender)"},
110
- :female => {:language_context => @context, :keyword => "female", :conditions => "(= 'female' @gender)"},
111
- :other => {:language_context => @context, :keyword => "other"}
112
- }
113
- )
114
-
115
- Tr8n.config.stub(:context_rules) {
116
- {
117
- "gender" => {
118
- "variables" => {
119
- "@gender" => "gender",
120
- }
121
- }
122
- }
123
- }
124
-
125
- expect(context.fallback_rule.keyword).to eq("other")
126
-
127
- expect(context.find_matching_rule(double(:gender => "male")).keyword).to eq('male')
128
- expect(context.find_matching_rule(double(:gender => "female")).keyword).to eq('female')
129
- expect(context.find_matching_rule(double(:gender => "unknown")).keyword).to eq('other')
130
-
131
- # unknown goes before other
132
- context.rules[:unknown] = Tr8n::LanguageContextRule.new(:language_context => @context, :keyword => "unknown", :conditions => "(&& (!= 'male' @gender) (!= 'female' @gender))")
133
- expect(context.find_matching_rule(double(:gender => "unknown")).keyword).to eq('unknown')
134
- end
135
- end
136
- end
137
-
138
- describe "genders rules" do
139
- before(:each) do
140
- @context = Tr8n::LanguageContext.new(
141
- :language => @english,
142
- :keyword => "genders",
143
- :token_expression => '/.*(profiles|users|actors|targets)(\d)*$/',
144
- :variables => ['@genders', '@count'],
145
- :description => "Language context for list of users"
146
- )
147
-
148
- Tr8n.config.stub(:context_rules) {
149
- {
150
- "genders" => {
151
- "variables" => {
152
- "@genders" => lambda{|list|
153
- list.collect{|u| u.gender}
154
- },
155
- "@count" => lambda{|list| list.count},
156
- }
157
- }
158
- }
159
- }
160
- end
161
-
162
- describe "loading variables" do
163
- it "should return assigned variables" do
164
- obj = [double(:gender => "male"), double(:gender => "female")]
165
- expect(@context.vars(obj)).to eq({"@genders" => ["male", "female"], "@count" => 2})
166
- end
167
- end
168
-
169
- describe "evaluate rules" do
170
- it "should return matching rule" do
171
- Tr8n.config.stub(:context_rules) {
172
- {
173
- "genders" => {
174
- "variables" => {
175
- "@genders" => lambda{|list| list.collect{|u| u.gender}},
176
- }
177
- }
178
- }
179
- }
180
-
181
- @context.rules[:one_male] = Tr8n::LanguageContextRule.new(:conditions => "(&& (= 1 (count @genders)) (all @genders 'male'))")
182
- @context.rules[:one_female] = Tr8n::LanguageContextRule.new(:conditions => "(&& (= 1 (count @genders)) (all @genders 'female'))")
183
- @context.rules[:one_unknown] = Tr8n::LanguageContextRule.new(:conditions => "(&& (= 1 (count @genders)) (all @genders 'unknown'))")
184
- @context.rules[:other] = Tr8n::LanguageContextRule.new(:keyword => "other")
185
-
186
- expect(@context.find_matching_rule([double(:gender => "male")])).to eq(@context.rules[:one_male])
187
- expect(@context.find_matching_rule([double(:gender => "female")])).to eq(@context.rules[:one_female])
188
- expect(@context.find_matching_rule([double(:gender => "unknown")])).to eq(@context.rules[:one_unknown])
189
- expect(@context.find_matching_rule([double(:gender => "male"), double(:gender => "male")])).to eq(@context.rules[:other])
190
-
191
- # unknown goes before other
192
- @context.rules[:at_least_two] = Tr8n::LanguageContextRule.new(:conditions => "(> (count @genders) 1)")
193
- expect(@context.find_matching_rule([double(:gender => "male"), double(:gender => "male")])).to eq(@context.rules[:at_least_two])
194
-
195
- @context.rules.delete(:at_least_two)
196
-
197
- @context.rules[:all_male] = Tr8n::LanguageContextRule.new(:conditions => "(&& (> (count @genders) 1) (all @genders 'male'))")
198
- @context.rules[:all_female] = Tr8n::LanguageContextRule.new(:conditions => "(&& (> (count @genders) 1) (all @genders 'female'))")
199
-
200
- expect(@context.find_matching_rule([double(:gender => "male"), double(:gender => "male")])).to eq(@context.rules[:all_male])
201
- expect(@context.find_matching_rule([double(:gender => "female"), double(:gender => "female")])).to eq(@context.rules[:all_female])
202
- expect(@context.find_matching_rule([double(:gender => "male"), double(:gender => "female")])).to eq(@context.rules[:other])
203
- end
204
- end
205
- end
206
-
207
- describe "list rules" do
208
- before(:each) do
209
- @context = Tr8n::LanguageContext.new(
210
- :language => @english,
211
- :keyword => "list",
212
- :token_expression => '/.*(list)(\d)*$/',
213
- :variables => ['@count'],
214
- :description => "Language context for lists"
215
- )
216
-
217
- Tr8n.config.stub(:context_rules) {
218
- {
219
- "list" => {
220
- "variables" => {
221
- "@count" => "count",
222
- }
223
- }
224
- }
225
- }
226
- end
227
-
228
- describe "loading variables" do
229
- it "should return assigned variables" do
230
- obj = ["apple", "banana"]
231
- expect(@context.vars(obj)).to eq({"@count" => 2})
232
- end
233
- end
234
-
235
- describe "evaluate rules" do
236
- it "should return matching rule" do
237
- @context.rules[:one] = Tr8n::LanguageContextRule.new(:conditions => "(= 1 @count)")
238
- @context.rules[:two] = Tr8n::LanguageContextRule.new(:conditions => "(= 2 @count)")
239
- @context.rules[:other] = Tr8n::LanguageContextRule.new(:keyword => "other")
240
-
241
- expect(@context.find_matching_rule(["apple"])).to eq(@context.rules[:one])
242
- expect(@context.find_matching_rule(["apple", "banana"])).to eq(@context.rules[:two])
243
- expect(@context.find_matching_rule(["apple", "banana", "grapes"])).to eq(@context.rules[:other])
244
- end
245
- end
246
- end
247
-
248
- describe "numeric rules" do
249
- before(:each) do
250
- @context = Tr8n::LanguageContext.new(
251
- :language => @english,
252
- :keyword => "numeric",
253
- :token_expression => '/.*(count|num|age|hours|minutes|years|seconds)(\d)*$/',
254
- :variables => ['@n'],
255
- :description => "Language context for numbers"
256
- )
257
-
258
- Tr8n.config.stub(:context_rules) {
259
- {
260
- "numeric" => {
261
- "token_expression" => '/.*(count|num|age|hours|minutes|years|seconds)(\d)*$/',
262
- "variables" => {
263
- "@n" => "to_i",
264
- }
265
- }
266
- }
267
- }
268
- end
269
-
270
- describe "loading variables" do
271
- it "should return assigned variables" do
272
- obj = double(:to_i => 10)
273
- expect(@context.vars(obj)).to eq({"@n" => 10})
274
-
275
- obj = 15
276
- expect(@context.vars(obj)).to eq({"@n" => 15})
277
- end
278
- end
279
-
280
- describe "evaluate rules" do
281
- it "should return matching rule" do
282
- @context.rules[:one] = Tr8n::LanguageContextRule.new(:conditions => "(&& (= 1 (mod @n 10)) (!= 11 (mod @n 100)))", :description => "{n} mod 10 is 1 and {n} mod 100 is not 11", :examples => "1, 21, 31, 41, 51, 61...")
283
- @context.rules[:few] = Tr8n::LanguageContextRule.new(:conditions => "(&& (in '2..4' (mod @n 10)) (not (in '12..14' (mod @n 100))))", :description => "{n} mod 10 in 2..4 and {n} mod 100 not in 12..14", :examples => "2-4, 22-24, 32-34...")
284
- @context.rules[:many] = Tr8n::LanguageContextRule.new(:conditions => "(|| (= 0 (mod @n 10)) (in '5..9' (mod @n 10)) (in '11..14' (mod @n 100)))", :description => "{n} mod 10 is 0 or {n} mod 10 in 5..9 or {n} mod 100 in 11..14", :examples => "0, 5-20, 25-30, 35-40...")
285
-
286
- expect(@context.find_matching_rule(1)).to eq(@context.rules[:one])
287
- expect(@context.find_matching_rule(2)).to eq(@context.rules[:few])
288
- expect(@context.find_matching_rule(0)).to eq(@context.rules[:many])
289
- end
290
- end
291
- end
292
-
293
- describe "date rules" do
294
- before(:each) do
295
- @context = Tr8n::LanguageContext.new(
296
- :language => @english,
297
- :keyword => "date",
298
- :token_expression => '/.*(date)(\d)*$/',
299
- :variables => ['@date'],
300
- :description => "Language context for dates"
301
- )
302
-
303
- Tr8n.config.stub(:context_rules) {
304
- {
305
- "date" => {
306
- "variables" => {
307
- }
308
- }
309
- }
310
- }
311
- end
312
-
313
- describe "loading variables" do
314
- it "should return assigned variables" do
315
- today = Date.today
316
- expect(@context.vars(today)).to eq({"@date" => today})
317
- end
318
- end
319
-
320
- describe "evaluate rules" do
321
- it "should return matching rule" do
322
- @context.rules[:present] = Tr8n::LanguageContextRule.new(:conditions => "(= (today) @date)")
323
- @context.rules[:past] = Tr8n::LanguageContextRule.new(:conditions => "(> (today) @date)")
324
- @context.rules[:future] = Tr8n::LanguageContextRule.new(:conditions => "(< (today) @date)")
325
-
326
- expect(@context.find_matching_rule(Date.today)).to eq(@context.rules[:present])
327
- expect(@context.find_matching_rule(Date.today - 24)).to eq(@context.rules[:past])
328
- expect(@context.find_matching_rule(Date.today + 24)).to eq(@context.rules[:future])
329
- end
330
- end
331
- end
332
-
333
- end