skala 0.2.0 → 0.3.0

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: 4cd834fb20b0e4148938d42dd92abecb54e49b8f
4
- data.tar.gz: d9c1fc1820efddffa3777811aa8eeffc6034ae5d
3
+ metadata.gz: aae9b8b40ca397d60ff19d4b4e18388bff0c0394
4
+ data.tar.gz: b560ab30285fab610ba6b04de36c21f94070983f
5
5
  SHA512:
6
- metadata.gz: 56346c0e4261433885405a632b7efc29b2561a6ac58ee7eb7e8cacc1118f62b9142217465696ccae3f36e7922776a98a219d1591df2dd2691691544940c61710
7
- data.tar.gz: bef839d6c64e4a345705109535e9cad8ff74e416871366837b0d5135ad6d2c467661ebc87a7c4ba83c2452d790789592da456490fa2afc3a371309a6cd7aa35c
6
+ metadata.gz: 66c797c3536ab3e72c8be2c57d2eb59585df9757387663c0d48a454f770297d111a8a440728fb2d7f625eb14666899713f52b1740bfeae8e55cbae433854fc99
7
+ data.tar.gz: 5fe5fd9cfdea4785ed5b1ef1866a7813dbeb80385eab89bec1ecd82734f3370e6a17e3d8c876a70b23d94f8eeecae1b672a4f7eeba31f6df4a301fa7cb18e978
data/lib/skala/adapter.rb CHANGED
@@ -5,8 +5,4 @@ class Skala::Adapter
5
5
  require_relative "./adapter/operation"
6
6
 
7
7
  include Skala::I18n
8
-
9
- def initialize(*args)
10
- self.class.load_locales_from_directory("#{File.dirname(__FILE__)}/locales")
11
- end
12
8
  end
data/lib/skala/i18n.rb CHANGED
@@ -1,42 +1,46 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "mighty_tap"
1
4
  require "yaml"
2
5
  require_relative "../skala"
3
6
 
4
7
  module Skala::I18n
5
- require_relative "./i18n/deep_merger"
6
-
7
8
  def self.included(klass)
8
- unless klass.class_variable_defined?(:@@locales)
9
- klass.class_variable_set(:@@locales, {})
10
- end
11
-
12
9
  klass.extend(ClassMethods)
13
10
  end
14
11
 
15
12
  module ClassMethods
16
13
  def load_locales_from_directory(path)
17
- # class_variable_get is needed to avoid implicite referencing the module instead of the including class
18
- Dir.glob("#{File.expand_path(path)}/*.yml").inject(self.class_variable_get(:@@locales)) do |locales, filename|
19
- DeepMerger.deep_merge!(locales, YAML.load_file(filename))
14
+ Dir.glob("#{File.expand_path(path)}/*.yml").inject({}) do |_locales, _filename|
15
+ _locales.mtap(:deep_merge!, YAML.load_file(_filename))
20
16
  end
21
17
  end
22
- end
23
18
 
24
- def translate(key, options = {})
25
- raise "Destination locale missing!" if options[:locale].nil?
26
-
27
- fully_qualified_key = "#{options[:locale]}.#{key}"
28
- keys_path = fully_qualified_key.split(".")
29
- locales = self.class.class_variable_get(:@@locales)
30
-
31
- translated_key = keys_path.inject(locales) do |hash, hash_key|
32
- unless hash.nil?
33
- hash[hash_key.to_s] || hash[hash_key.to_sym]
34
- end
35
- end || keys_path.last
19
+ # override this to provide locales
20
+ def locales
21
+ {}
22
+ end
23
+
24
+ def translate(key, options = {})
25
+ raise "Destination locale missing!" if options[:locale].nil?
26
+
27
+ fully_qualified_key = "#{options[:locale]}.#{key}"
28
+ keys_path = fully_qualified_key.split(".")
29
+
30
+ translated_key = keys_path.inject(locales) do |hash, hash_key|
31
+ unless hash.nil?
32
+ hash[hash_key.to_s] || hash[hash_key.to_sym]
33
+ end
34
+ end || keys_path.last
36
35
 
37
- translated_key.gsub(/%{[^}]+}+/) do |match|
38
- interpolation_key = match.gsub(/(\A%{)|(}\Z)/, "")
39
- options[interpolation_key.to_s] || options[interpolation_key.to_sym] || match
36
+ translated_key.gsub(/%{[^}]+}+/) do |match|
37
+ interpolation_key = match.gsub(/(\A%{)|(}\Z)/, "")
38
+ options[interpolation_key.to_s] || options[interpolation_key.to_sym] || match
39
+ end
40
40
  end
41
41
  end
42
+
43
+ def translate(*args)
44
+ self.class.translate(*args)
45
+ end
42
46
  end
data/lib/skala/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Skala
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/skala.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_dependency "activesupport"
20
+ spec.add_dependency "mighty_tap"
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.7"
22
23
  spec.add_development_dependency "ox", ">= 2.1.0"
@@ -1,30 +1,2 @@
1
1
  describe Skala::Adapter do
2
- let(:adapter) { described_class.new() }
3
-
4
- context "when a class is derived from #{described_class}" do
5
- context "when the derived class overwrites .initialize" do
6
- context "when the derived class calls super inside the overwritten .initalize" do
7
- let(:derived_class) do
8
- Class.new(described_class) do
9
- def initialize
10
- super
11
-
12
- self.class.load_locales_from_directory(File.expand_path("#{File.dirname(__FILE__)}/../assets/locales"))
13
- end
14
- end
15
- end
16
-
17
- let(:derived_class_instance) do
18
- derived_class.new
19
- end
20
-
21
- it "has access to the superclass locales" do
22
- translation_key_known_by_superclass = "field_names.creator"
23
- translation = derived_class_instance.translate(translation_key_known_by_superclass, locale: :de)
24
- expected_translation = derived_class_instance.class.class_variable_get(:@@locales)["de"]["field_names"]["creator"]
25
- expect(translation).to eq(expected_translation)
26
- end
27
- end
28
- end
29
- end
30
2
  end
@@ -6,86 +6,89 @@ describe Skala::I18n do
6
6
  end
7
7
 
8
8
  context "when included into a class" do
9
- context "when the including class has a class variable named @@locales" do
10
- let(:value_of_locales) do
11
- { foo: "bar" }
12
- end
13
-
14
- let(:klass) do
15
- Class.new.tap do |_klass|
16
- _klass.class_variable_set(:@@locales, value_of_locales)
17
- _klass.send(:include, described_class)
18
- end
19
- end
20
-
21
- it "does not alter the existing class variable" do
22
- expect(klass.class_variable_get(:@@locales)).to eq(value_of_locales)
23
- end
9
+ it "defines a class method #load_locales_from_directory" do
10
+ expect(class_with_i18n_included).to respond_to(:load_locales_from_directory)
24
11
  end
25
12
 
26
- context "when the including class has no class variable named @@locales" do
27
- let(:klass) do
28
- Class.new.tap do |_klass|
29
- _klass.send(:include, described_class)
30
- end
31
- end
32
-
33
- it "defines a class variable named @@locales as an empty hash" do
34
- expect(klass.class_variable_get(:@@locales)).to eq({})
35
- end
13
+ it "defines a class method #locales" do
14
+ expect(class_with_i18n_included).to respond_to(:locales)
36
15
  end
37
16
 
38
- it "defines a class method #load_locales_from_directory" do
39
- expect(class_with_i18n_included).to respond_to(:load_locales_from_directory)
17
+ it "defines a class method #translate" do
18
+ expect(class_with_i18n_included).to respond_to(:translate)
40
19
  end
41
-
20
+
42
21
  it "defines an instance method .translate" do
43
22
  expect(class_with_i18n_included.new).to respond_to(:translate)
44
23
  end
45
24
  end
46
25
 
47
26
  describe "#load_locales_from_directory" do
48
- it "loads all yaml encoded locales from a directory and adds them to @@locales" do
49
- class_with_i18n_included.load_locales_from_directory(File.expand_path("#{File.dirname(__FILE__)}/../assets/locales"))
50
- expect(class_with_i18n_included.class_variable_get(:@@locales)).not_to be_empty
27
+ it "loads all yaml encoded locales from a directory" do
28
+ loaded_locales =
29
+ class_with_i18n_included.load_locales_from_directory(
30
+ File.expand_path("#{File.dirname(__FILE__)}/../assets/locales")
31
+ )
32
+
33
+ expect(loaded_locales).not_to be_empty
51
34
  end
52
35
  end
53
-
36
+
54
37
  describe "#translate" do
55
- let(:object) do
56
- class_with_i18n_included.new.tap do |_object|
57
- _object.class.load_locales_from_directory(File.expand_path("#{File.dirname(__FILE__)}/../assets/locales"))
38
+ context "if the class defines .locales" do
39
+ let(:class_with_i18n_included_and_locales) do
40
+ Class.new(class_with_i18n_included).tap do |_klass|
41
+ def _klass.locales
42
+ load_locales_from_directory(
43
+ File.expand_path("#{File.dirname(__FILE__)}/../assets/locales")
44
+ )
45
+ end
46
+ end
58
47
  end
59
- end
60
48
 
61
- it "translates the given key into the language specified by the locale option" do
62
- translated_key_de = object.translate("field_names.creator", locale: :de)
63
- translated_key_en = object.translate("field_names.creator", locale: :en)
64
-
65
- expected_translation_de = object.class.class_variable_get(:@@locales)["de"]["field_names"]["creator"]
66
- expected_translation_en = object.class.class_variable_get(:@@locales)["en"]["field_names"]["creator"]
49
+ let(:object) do
50
+ class_with_i18n_included_and_locales.new
51
+ end
67
52
 
68
- expect(translated_key_de).to eq(expected_translation_de)
69
- expect(translated_key_en).to eq(expected_translation_en)
70
- end
53
+ it "translates the given key into the language specified by the locale option" do
54
+ translated_key_de = object.translate("field_names.creator", locale: :de)
55
+ translated_key_en = object.translate("field_names.creator", locale: :en)
56
+
57
+ expected_translation_de = object.class.locales["de"]["field_names"]["creator"]
58
+ expected_translation_en = object.class.locales["en"]["field_names"]["creator"]
71
59
 
72
- it "supports rails style interpolation" do
73
- expected_date = "1. Januar 2015"
74
- translated_key = object.translate("records.items.loan_status.expected", locale: :de, expected_date: expected_date)
75
- expected_translation = object.class.class_variable_get(:@@locales)["de"]["records"]["items"]["loan_status"]["expected"].gsub("%{expected_date}", expected_date)
60
+ expect(translated_key_de).to eq(expected_translation_de)
61
+ expect(translated_key_en).to eq(expected_translation_en)
62
+ end
63
+
64
+ it "supports rails style interpolation" do
65
+ expected_date = "1. Januar 2015"
66
+ translated_key = object.translate("records.items.loan_status.expected", locale: :de, expected_date: expected_date)
67
+ expected_translation = object.class.locales["de"]["records"]["items"]["loan_status"]["expected"].gsub("%{expected_date}", expected_date)
76
68
 
77
- expect(translated_key).to eq(expected_translation)
78
- end
69
+ expect(translated_key).to eq(expected_translation)
70
+ end
71
+
72
+ context "when there is no translation" do
73
+ it "returns the last key path" do
74
+ expect(object.translate("foo.bar.muff", locale: :en)).to eq("muff")
75
+ end
76
+ end
79
77
 
80
- context "when there is no translation" do
81
- it "returns to last key path" do
82
- expect(object.translate("foo.bar.muff", locale: :en)).to eq("muff")
78
+ context "when no locale option is given" do
79
+ it "raises an exception" do
80
+ expect { object.translate("field_names.creator") }.to raise_error
81
+ end
83
82
  end
84
83
  end
85
84
 
86
- context "when no locale option is given" do
87
- it "raises an exception" do
88
- expect { object.translate("field_names.creator") }.to raise_error
85
+ context "if the class does not define .locales" do
86
+ let(:object) do
87
+ class_with_i18n_included.new
88
+ end
89
+
90
+ it "returns the last key path" do
91
+ expect(object.translate("foo.bar.muff", locale: :en)).to eq("muff")
89
92
  end
90
93
  end
91
94
  end
@@ -27,20 +27,29 @@ describe Skala::Transformation do
27
27
  describe "#abort!" do
28
28
  let(:transformation) do
29
29
  Class.new(described_class) do
30
+ def abort_execution?
31
+ !!@abort_execution
32
+ end
33
+
34
+ def initialize(abort_execution = true)
35
+ @abort_execution = abort_execution
36
+ end
37
+
30
38
  sequence [
31
39
  -> (transformation) do
32
40
  transformation.target = "first_steps_output"
33
- transformation.abort!
41
+ transformation.abort! if transformation.abort_execution?
34
42
  end,
35
43
  -> (transformation) do
36
44
  transformation.target = "second_steps_output"
37
45
  end
38
46
  ]
39
- end.new
47
+ end
40
48
  end
41
49
 
42
50
  it "aborts the execution of the steps sequence" do
43
- expect(transformation.apply to: {}).to eq("first_steps_output")
51
+ expect(transformation.new.apply to: {}).to eq("first_steps_output")
52
+ expect(transformation.new(false).apply to: {}).to eq("second_steps_output")
44
53
  end
45
54
  end
46
55
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skala
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-15 00:00:00.000000000 Z
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mighty_tap
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -157,7 +171,6 @@ files:
157
171
  - lib/skala/adapter.rb
158
172
  - lib/skala/adapter/operation.rb
159
173
  - lib/skala/i18n.rb
160
- - lib/skala/i18n/deep_merger.rb
161
174
  - lib/skala/transformation.rb
162
175
  - lib/skala/transformation/step.rb
163
176
  - lib/skala/version.rb
@@ -167,7 +180,6 @@ files:
167
180
  - spec/assets/transformation/some_class.rb
168
181
  - spec/skala/adapter/operation_spec.rb
169
182
  - spec/skala/adapter_spec.rb
170
- - spec/skala/i18n/deep_merger_spec.rb
171
183
  - spec/skala/i18n_spec.rb
172
184
  - spec/skala/transformation/step_spec.rb
173
185
  - spec/skala/transformation_spec.rb
@@ -203,7 +215,6 @@ test_files:
203
215
  - spec/assets/transformation/some_class.rb
204
216
  - spec/skala/adapter/operation_spec.rb
205
217
  - spec/skala/adapter_spec.rb
206
- - spec/skala/i18n/deep_merger_spec.rb
207
218
  - spec/skala/i18n_spec.rb
208
219
  - spec/skala/transformation/step_spec.rb
209
220
  - spec/skala/transformation_spec.rb
@@ -1,45 +0,0 @@
1
- require_relative "../i18n"
2
-
3
- module Skala::I18n::DeepMerger
4
- def self.deep_merge!(destination, source, options = {})
5
- if destination.kind_of?(Array)
6
- if source.kind_of?(Array)
7
- # array into array
8
- source.each do |element|
9
- self.deep_merge!(destination, element, options)
10
- end
11
- elsif source.kind_of?(Hash)
12
- # hash into array
13
- destination.push(source)
14
- else
15
- # literal into array
16
- destination.push(source)
17
- end
18
- elsif destination.kind_of?(Hash)
19
- if source.kind_of?(Array)
20
- # array into hash
21
- raise ArgumentError, "Cannot merge array into hash!"
22
- elsif source.kind_of?(Hash)
23
- # hash into hash
24
- destination.merge!(source) do |key, destination_value, source_value|
25
- self.deep_merge!(destination_value, source_value, options)
26
- destination_value
27
- end
28
- else
29
- # literal into hash
30
- raise ArgumentError, "Cannot merge literal into hash!"
31
- end
32
- else
33
- if source.kind_of?(Array)
34
- # array into literal
35
- raise ArgumentError, "Cannot merge array into literal!"
36
- elsif source.kind_of?(Hash)
37
- # hash into literal
38
- raise ArgumentError, "Cannot merge hash into literal!"
39
- else
40
- # literal into literal
41
- destination = source # in fact a useless assignment to fit into the overall semantic
42
- end
43
- end
44
- end
45
- end
@@ -1,35 +0,0 @@
1
- describe Skala::I18n::DeepMerger do
2
- describe ".deep_merge!" do
3
- describe "merges arrays into arrays" do
4
- it "merges two arrays of literals" do
5
- source = [1,"2",:f]
6
- destination = [:"3", 5, "foo"]
7
- described_class.deep_merge!(destination, source)
8
- expect(destination).to eq([:"3", 5, "foo", 1, "2", :f])
9
- end
10
-
11
- it "merges two arrays of hashes" do
12
- source = [{c:3}]
13
- destination = [{a: 1}, {b:2}]
14
- described_class.deep_merge!(destination, source)
15
- expect(destination).to eq([{:a=>1}, {:b=>2}, {:c=>3}])
16
- end
17
- end
18
-
19
- describe "merges hashes into hashes" do
20
- it "merges two simple hashes" do
21
- source = { c: 3 }
22
- destination = { a: 1, b: 2 }
23
- described_class.deep_merge!(destination, source)
24
- expect(destination).to eq({:a=>1, :b=>2, :c=>3})
25
- end
26
-
27
- it "merges two nested hashes" do
28
- source = { a: { type: "multi_value", values: [2,3] }, b: { value: "123" } }
29
- destination = { a: { values: [1]}, b: { type: "id" } }
30
- described_class.deep_merge!(destination, source)
31
- expect(destination).to eq({:a=>{:values=>[1, 2, 3], :type=>"multi_value"}, :b=>{:type=>"id", :value=>"123"}})
32
- end
33
- end
34
- end
35
- end