tr8n_core 4.0.2 → 4.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a3d5a8a77266054697c3f8dcd30f7fd59a8c563
4
- data.tar.gz: 244e312b0f03a9f6f6b232f5357a9b0c7991316d
3
+ metadata.gz: 176e098158a14899141b0806820928ab5a5dca78
4
+ data.tar.gz: ec746b4fb3729d02b5689ba15aa6639403977b99
5
5
  SHA512:
6
- metadata.gz: 06e875646e358cd4a3228268d594eec6cc105b5c32176c6b430024de3c806856226b0833a329b060541628803213a8f032a76e2f8eba60e7b84b8291e4957aaf
7
- data.tar.gz: d11ddd0b0f3d64bdb7abdfba80c1dcfb4bcc45453c1ca5847bd6076fe7ad7dae124b66bf71562c03ba16a22642890d0e6deea3ccf2e1bda29d5bddbe2134ede4
6
+ metadata.gz: bbec300dd025ef1981175efb9b245115c320080bacd001efb8ed2cd5ee0aff0d0ae66a9fd4f6c1a84b40f583a4cb0324bbdfb04e89437e17a79e2bf48033c22d
7
+ data.tar.gz: 3a80ed12cccb3ebe9d34ff032fafc59d8b7d8ce5db78f11abe15d23236c2cc33d409718ff03ac12a017b477fbfb5d140d4b5ea89d1a8fb2ec2138165143ab59b
data/lib/tr8n/cache.rb CHANGED
@@ -66,7 +66,7 @@ module Tr8n
66
66
  end
67
67
 
68
68
  def cache_name
69
- self.class.name.underscore.split('_').last
69
+ self.class.name.split('::').last
70
70
  end
71
71
 
72
72
  def info(msg)
@@ -29,6 +29,10 @@ class Tr8n::CacheAdapters::Cdb < Tr8n::Cache
29
29
  @cache = LibCDB::CDB.open(cache_path)
30
30
  end
31
31
 
32
+ def cache_name
33
+ "cdb"
34
+ end
35
+
32
36
  def self.cache_path
33
37
  "#{Tr8n.config.cache[:path]}/cdb/current.cdb"
34
38
  end
@@ -35,6 +35,10 @@ class Tr8n::CacheAdapters::File < Tr8n::Cache
35
35
  "#{cache_path}/#{file_name(key)}"
36
36
  end
37
37
 
38
+ def cache_name
39
+ "file"
40
+ end
41
+
38
42
  def fetch(key, opts = {})
39
43
  path = self.class.file_path(key)
40
44
 
@@ -60,7 +64,7 @@ class Tr8n::CacheAdapters::File < Tr8n::Cache
60
64
  end
61
65
 
62
66
  def exist?(key, opts = {})
63
- File.exists(file_path(key))
67
+ File.exists?(self.class.file_path(key))
64
68
  end
65
69
 
66
70
  def clear(opts = {})
@@ -26,10 +26,14 @@ require 'dalli' if defined?(Dalli)
26
26
  class Tr8n::CacheAdapters::Memcache < Tr8n::Cache
27
27
 
28
28
  def initialize
29
- options = { :namespace => "tr8n", :compress => true }
29
+ options = { :namespace => Tr8n.config.cache[:namespace] || "tr8n", :compress => Tr8n.config.cache[:compress].nil? ? true : Tr8n.config.cache[:compress]}
30
30
  @cache = Dalli::Client.new(Tr8n.config.cache[:host], options)
31
31
  end
32
32
 
33
+ def cache_name
34
+ "memcache"
35
+ end
36
+
33
37
  def read_only?
34
38
  false
35
39
  end
@@ -33,6 +33,10 @@ class Tr8n::CacheAdapters::Redis < Tr8n::Cache
33
33
  @cache = ::Redis.new(host: cache_host, port: cache_port)
34
34
  end
35
35
 
36
+ def cache_name
37
+ "redis"
38
+ end
39
+
36
40
  def read_only?
37
41
  false
38
42
  end
data/lib/tr8n/config.rb CHANGED
@@ -58,7 +58,7 @@ module Tr8n
58
58
  # end
59
59
  #
60
60
 
61
- def with_config_settings
61
+ def self.with_config_settings
62
62
  old_config = @config.dup
63
63
  yield(@config)
64
64
  @config = old_config
@@ -88,11 +88,13 @@ module Tr8n
88
88
  @current_locale_method = :current_locale
89
89
  @current_user_method = :current_user
90
90
 
91
- @application = {
92
- :host => 'http://localhost:3000',
93
- :key => 'default',
94
- :secret => '12345',
95
- }
91
+ #@application = {
92
+ # :host => 'https://localhost:3000',
93
+ # :key => 'default',
94
+ # :secret => '12345',
95
+ #}
96
+
97
+ @application = nil
96
98
 
97
99
  @context_rules = {
98
100
  :number => {
data/lib/tr8n/language.rb CHANGED
@@ -151,7 +151,7 @@ class Tr8n::Language < Tr8n::Base
151
151
 
152
152
  # if it came from cache, it will be full of translation keys with translations for the locale
153
153
  if source
154
- translation_keys = source.translation_keys
154
+ translation_keys = source.translation_keys || {}
155
155
  elsif Tr8n.cache.read_only?
156
156
  translation_keys = {}
157
157
  else
data/lib/tr8n/session.rb CHANGED
@@ -35,7 +35,11 @@ module Tr8n
35
35
  :current_source, :current_component, :block_options
36
36
 
37
37
  def init
38
- Tr8n::Application.init(Tr8n.config.application[:key], Tr8n.config.application[:secret], Tr8n.config.application[:host])
38
+ return unless Tr8n.config.application # not configured
39
+
40
+ unless @application
41
+ Tr8n::Application.init(Tr8n.config.application[:key], Tr8n.config.application[:secret], Tr8n.config.application[:host])
42
+ end
39
43
  self.current_source = "/tr8n/core"
40
44
  end
41
45
 
@@ -63,7 +67,7 @@ module Tr8n
63
67
  if block_given?
64
68
  ret = yield
65
69
  end
66
- @block_options.pop
70
+ @block_options.pop if @block_options
67
71
  ret
68
72
  end
69
73
 
data/lib/tr8n/source.rb CHANGED
@@ -27,10 +27,10 @@ class Tr8n::Source < Tr8n::Base
27
27
  has_many :translation_keys
28
28
 
29
29
  def self.normalize(url)
30
- return nil if url.blank?
30
+ return nil if url.nil? or url == ""
31
31
  uri = URI.parse(url)
32
32
  path = uri.path
33
- return "/" if uri.path.blank?
33
+ return "/" if uri.path.nil? or uri.path == ""
34
34
  return path if path == "/"
35
35
 
36
36
  # always must start with /
@@ -23,30 +23,6 @@
23
23
 
24
24
  class Hash
25
25
 
26
- # Return all combinations of a hash.
27
- #
28
- # Example:
29
- # {
30
- # :a => [1, 2]
31
- # :b => [1, 2]
32
- # }.combinations #=> [{:a=>1, :b=>1}, {:a=>1, :b=>2}, {:a=>2, :b=>1}, {:a=>2, :b=>2}]
33
- #
34
- def combinations
35
- return [{}] if empty?
36
-
37
- copy = dup
38
- values = copy.delete(key = keys.first)
39
-
40
- result = []
41
- copy.combinations.each do |tail|
42
- values.each do |value|
43
- result << tail.merge(key=>value)
44
- end
45
- end
46
-
47
- result
48
- end
49
-
50
26
  def tr8n_translated
51
27
  return self if frozen?
52
28
  @tr8n_translated = true
@@ -23,8 +23,10 @@
23
23
 
24
24
  class Tr8nCore::Generators::Cache::Base
25
25
 
26
+ attr_accessor :started_at, :finished_at
27
+
26
28
  def log(msg)
27
- puts("#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}: #{msg}\n")
29
+ Tr8n.logger.debug("#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}: #{msg}\n")
28
30
  end
29
31
 
30
32
  def cache_path
@@ -26,16 +26,19 @@ class Tr8nCore::Generators::Cache::File < Tr8nCore::Generators::Cache::Base
26
26
  def cache_path
27
27
  @cache_path ||= begin
28
28
  path = "#{Tr8n.config.cache[:path]}/files/tr8n_#{Tr8n.session.application.key}_#{@started_at.strftime('%Y_%m_%d_%H_%M_%S')}"
29
- pp "Cache will be stored in #{path}"
29
+ log("Cache will be stored in #{path}")
30
30
  FileUtils.mkdir_p(path)
31
31
  FileUtils.chmod(0777, path)
32
32
  path
33
33
  end
34
34
  end
35
35
 
36
+ def file_path(key)
37
+ "#{cache_path}/#{Tr8n::CacheAdapters::File.file_name(key)}"
38
+ end
39
+
36
40
  def cache(key, data)
37
- file_path = "#{cache_path}/#{Tr8n::CacheAdapters::File.file_name(key)}"
38
- File.open(file_path, 'w') { |file| file.write(JSON.pretty_generate(data)) }
41
+ File.open(file_path(key), 'w') { |file| file.write(JSON.pretty_generate(data)) }
39
42
  end
40
43
 
41
44
  def symlink_path
@@ -23,5 +23,5 @@
23
23
 
24
24
 
25
25
  module Tr8nCore
26
- VERSION = "4.0.2"
26
+ VERSION = "4.0.4"
27
27
  end
@@ -36,6 +36,17 @@ describe Tr8n::Application do
36
36
  expect(russian.contexts.keys.size).to eq(6)
37
37
  expect(russian.contexts.keys).to eq(["date", "gender", "genders", "list", "number", "value"])
38
38
  end
39
+
40
+ it "should reset translations" do
41
+ @app.reset_translation_cache
42
+ expect(@app.translation_keys).to eq({})
43
+ end
44
+
45
+ it "should reset translations" do
46
+ @app.register_missing_key(Tr8n::TranslationKey.new(:application => @app, :label => "Hello"), Tr8n::Source.new(:key => "test"))
47
+ end
48
+
39
49
  end
40
50
 
51
+
41
52
  end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tr8n::CacheAdapters::File do
6
+ describe "#configuration" do
7
+ it "provides correct values" do
8
+
9
+ Tr8n.with_config_settings do |config|
10
+
11
+ config.cache = {
12
+ :path => "./cache"
13
+ }
14
+
15
+ c = Tr8n::CacheAdapters::File.new
16
+
17
+ expect(c.class.cache_path).to eq("./cache/files/current")
18
+
19
+ expect(c.class.file_name("test_key")).to eq("test_key.json")
20
+ expect(c.class.file_name("test/key")).to eq("test-key.json")
21
+ expect(c.class.file_path("test/key")).to eq("./cache/files/current/test-key.json")
22
+
23
+ c.store("a", "b")
24
+ c.delete("a")
25
+ c.clear()
26
+
27
+ expect(c.exist?("some_new_key")).to be_false
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tr8n::CacheAdapters::Memcache do
6
+ describe "#configuration" do
7
+ it "provides correct values" do
8
+
9
+ Tr8n.with_config_settings do |config|
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tr8nCore::Generators::Cache::File do
6
+ describe "#configuration" do
7
+ it "provides correct values" do
8
+
9
+ Tr8n.with_config_settings do |config|
10
+ Tr8n.session.application = init_application
11
+
12
+ config.cache = {
13
+ :path => "./cache"
14
+ }
15
+
16
+ g = Tr8nCore::Generators::Cache::File.new
17
+ g.started_at = Time.new(2014, 01, 01, 10, 11, 12)
18
+ expect(g.cache_path).to eq("./cache/files/tr8n_default_2014_01_01_10_11_12")
19
+ expect(g.symlink_path).to eq("./cache/files/current")
20
+
21
+ expect(g.file_path("test_key")).to eq("./cache/files/tr8n_default_2014_01_01_10_11_12/test_key.json")
22
+
23
+ g.cache("test_key", {"a" => "b"})
24
+ expect(File.exists?(g.file_path("test_key"))).to be_true
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Array do
6
+ it "must provide correct attributes" do
7
+ expect([].tr8n_translated?).to be_nil
8
+
9
+ a = [].tr8n_translated
10
+ expect(a.tr8n_translated?).to be_true
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Hash do
6
+ it "must provide correct attributes" do
7
+ h = {}.tr8n_translated
8
+ expect(h.tr8n_translated?).to be_true
9
+
10
+ h1 = {"a" => 100, "b" => 200, "c" => {"c1" => 12, "c2" => 14}}
11
+ h2 = {"b" => 254, "c" => 300, "c" => {"c1" => 16, "c3" => 94}}
12
+ expect(h1.rmerge(h2)).to eq({"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}})
13
+ expect(h1.rmerge!(h2)).to eq({"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}})
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe String do
6
+ it "must provide correct attributes" do
7
+ s = "".tr8n_translated
8
+ expect(s.tr8n_translated?).to be_true
9
+ end
10
+ end
@@ -18,6 +18,8 @@ describe Tr8n::Language do
18
18
  expect(@russian.class.cache_key("ru")).to eq("l@_[ru]")
19
19
  expect(@russian.class.cache_prefix).to eq("l@")
20
20
 
21
+ expect(@russian.has_definition?).to be_true
22
+
21
23
  end
22
24
  end
23
25
 
@@ -387,8 +389,6 @@ describe Tr8n::Language do
387
389
  Tr8n.session.with_block_options(:dry => true) do
388
390
 
389
391
  time = Time.new(2014, 01, 02, 11, 12, 13)
390
- expect(@english.translate("This message was received at {time}", :time => time)).to eq("This message was received at 2014-01-02 11:12:13 -0800")
391
-
392
392
 
393
393
  expect(time.translate()).to eq("1/2/2014")
394
394
  expect(time.tr()).to eq("1/2/2014")
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tr8n::Logger do
6
+ it "must provide correct data" do
7
+ Tr8n.logger.trace_api_call("/test", {}) do
8
+ # do nothing
9
+ end
10
+
11
+ Tr8n.logger.trace("Testing code") do
12
+ # do nothing
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Tr8n::Source do
6
+ describe "#initialize" do
7
+ it "helper methods" do
8
+ expect(Tr8n::Source.normalize("https://travis-ci.org/tr8n/tr8n_ruby_core")).to eq("/tr8n/tr8n_ruby_core")
9
+ expect(Tr8n::Source.normalize("https://www.google.com/search?q=test&source=lnms")).to eq("/search")
10
+ expect(Tr8n::Source.cache_prefix).to eq('s@')
11
+ end
12
+ end
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  require 'rspec'
2
4
  require 'json'
3
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tr8n_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.2
4
+ version: 4.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-09 00:00:00.000000000 Z
11
+ date: 2014-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -80,11 +80,17 @@ files:
80
80
  - README.md
81
81
  - spec/application_spec.rb
82
82
  - spec/base_spec.rb
83
+ - spec/cache/adapters/file_spec.rb
84
+ - spec/cache/adapters/memcache_spec.rb
85
+ - spec/cache/generators/file_generator_spec.rb
83
86
  - spec/config_spec.rb
84
87
  - spec/decorator_spec.rb
85
88
  - spec/decorators/base_spec.rb
86
89
  - spec/decorators/default_spec.rb
87
90
  - spec/decorators/html_spec.rb
91
+ - spec/ext/array_spec.rb
92
+ - spec/ext/hash_spec.rb
93
+ - spec/ext/string_spec.rb
88
94
  - spec/fixtures/application.json
89
95
  - spec/fixtures/languages/en-US.json
90
96
  - spec/fixtures/languages/es.json
@@ -100,8 +106,10 @@ files:
100
106
  - spec/language_context_rule_spec.rb
101
107
  - spec/language_context_spec.rb
102
108
  - spec/language_spec.rb
109
+ - spec/logger_spec.rb
103
110
  - spec/rules_engine/evaluator_spec.rb
104
111
  - spec/rules_engine/parser_spec.rb
112
+ - spec/source_spec.rb
105
113
  - spec/spec_helper.rb
106
114
  - spec/tokens/data_spec.rb
107
115
  - spec/tokens/data_tokenizer_spec.rb
@@ -112,7 +120,7 @@ files:
112
120
  - spec/translation_key_spec.rb
113
121
  - spec/translation_spec.rb
114
122
  - spec/utils_spec.rb
115
- homepage: https://tr8nhub.com
123
+ homepage: https://github.com/tr8n/tr8n_ruby_core
116
124
  licenses:
117
125
  - MIT-LICENSE
118
126
  metadata: {}
@@ -139,11 +147,17 @@ summary: Tr8n Core Classes
139
147
  test_files:
140
148
  - spec/application_spec.rb
141
149
  - spec/base_spec.rb
150
+ - spec/cache/adapters/file_spec.rb
151
+ - spec/cache/adapters/memcache_spec.rb
152
+ - spec/cache/generators/file_generator_spec.rb
142
153
  - spec/config_spec.rb
143
154
  - spec/decorator_spec.rb
144
155
  - spec/decorators/base_spec.rb
145
156
  - spec/decorators/default_spec.rb
146
157
  - spec/decorators/html_spec.rb
158
+ - spec/ext/array_spec.rb
159
+ - spec/ext/hash_spec.rb
160
+ - spec/ext/string_spec.rb
147
161
  - spec/fixtures/application.json
148
162
  - spec/fixtures/languages/en-US.json
149
163
  - spec/fixtures/languages/es.json
@@ -159,8 +173,10 @@ test_files:
159
173
  - spec/language_context_rule_spec.rb
160
174
  - spec/language_context_spec.rb
161
175
  - spec/language_spec.rb
176
+ - spec/logger_spec.rb
162
177
  - spec/rules_engine/evaluator_spec.rb
163
178
  - spec/rules_engine/parser_spec.rb
179
+ - spec/source_spec.rb
164
180
  - spec/spec_helper.rb
165
181
  - spec/tokens/data_spec.rb
166
182
  - spec/tokens/data_tokenizer_spec.rb