babylonia 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +11 -0
- data/Gemfile.lock +1 -1
- data/lib/babylonia/class_methods.rb +23 -2
- data/lib/babylonia/version.rb +1 -1
- data/spec/babylonia/class_methods_spec.rb +26 -0
- metadata +2 -2
- data/README.md +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a13bf3ea4fe0951e8ee79c6a22787739541c1225
|
4
|
+
data.tar.gz: f92431777d5a4df6fb6068ec6c4fbd37a5703d37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e61ded2cb171b09824548fb281b768ae98d6a8289618613607cd37af950fb7876ed8046ece7cbe6f5c2c87ee2a70ef8ebc1094a447b07e9c706af395800d1257
|
7
|
+
data.tar.gz: 3d434f2287d4e7da7c80af4815fc00a3faae7f866c39b0b3ad079fb4c7dd5a077d023094fb486905370cd5e723b248a84abbdb83497c8979f9116534c68959aa
|
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
@@ -3,16 +3,22 @@ require 'i18n'
|
|
3
3
|
|
4
4
|
module Babylonia
|
5
5
|
module ClassMethods
|
6
|
+
|
6
7
|
def build_babylonian_tower_on(*fields)
|
7
8
|
babylonian_options = fields.last.is_a?(Hash) ? fields.pop : {}
|
8
9
|
babylonian_options[:locale] ||= lambda { |r, f| I18n.locale }
|
9
10
|
babylonian_options[:default_locale] ||= lambda { |r, f| I18n.default_locale }
|
10
11
|
babylonian_options[:fallback] = true if babylonian_options[:fallback].nil?
|
11
12
|
|
12
|
-
# loop through fields to define methods such as "name" and "description"
|
13
13
|
fields.each do |field|
|
14
14
|
instance_variable_set(:"@babylonian_options_for_#{field}", babylonian_options)
|
15
15
|
|
16
|
+
# Alias method chain the field to a translated value
|
17
|
+
# @param [Symbol] locale Pass a locale to get the field translation in this specific locale
|
18
|
+
# @return [String, NilClass] Either the string with the translation, the fallback, the placeholder or nil
|
19
|
+
# @example Call a field in italian
|
20
|
+
# your_instance.field(:it) #=> "Translation"
|
21
|
+
#
|
16
22
|
define_method :"#{field}_translated" do |locale=nil|
|
17
23
|
field_hash = send(:"#{field}_hash")
|
18
24
|
translation = field_hash[locale || evaluate_babylonian_option!(:locale, field)]
|
@@ -22,15 +28,30 @@ module Babylonia
|
|
22
28
|
alias_method :"#{field}_raw", field
|
23
29
|
alias_method field, :"#{field}_translated"
|
24
30
|
|
31
|
+
# Return the translated values as a hash
|
32
|
+
# @return [Hash] The hash with all the translations stored in the field
|
33
|
+
#
|
25
34
|
define_method :"#{field}_hash" do
|
26
35
|
field_content = send(:"#{field}_raw")
|
27
36
|
field_content.is_a?(String) ? YAML.load(field_content) : {}
|
28
37
|
end
|
29
38
|
|
39
|
+
# Return all the languages stored
|
40
|
+
# @return [Array] An array containing all languages stored in the field
|
41
|
+
#
|
30
42
|
define_method :"#{field}_languages" do
|
31
43
|
send(:"#{field}_hash").keys
|
32
44
|
end
|
33
45
|
|
46
|
+
# Set the field to a value. This either takes a string or a hash
|
47
|
+
# If given a String, the current locale is set to this value
|
48
|
+
# If given a Hash, the hash is merged into the current translation hash, and empty values are purged
|
49
|
+
# @param [String, Hash] data The data to set either the current language translation or all translations to
|
50
|
+
# @example Set the translation for the current locale
|
51
|
+
# your_object.field = "TRANSLATION"
|
52
|
+
# @example Set the translation and delete italian
|
53
|
+
# your_object.field = {de: 'DEUTSCH', it: ''}
|
54
|
+
#
|
34
55
|
define_method :"#{field}_translated=" do |data|
|
35
56
|
current_hash = send(:"#{field}_hash")
|
36
57
|
|
@@ -40,7 +61,7 @@ module Babylonia
|
|
40
61
|
current_hash.merge! data
|
41
62
|
end
|
42
63
|
|
43
|
-
send(:"#{field}_raw=", YAML.dump(current_hash))
|
64
|
+
send(:"#{field}_raw=", YAML.dump(current_hash.delete_if{|k,v| v.nil? || v.empty? }))
|
44
65
|
end
|
45
66
|
alias_method :"#{field}_raw=", :"#{field}="
|
46
67
|
alias_method :"#{field}=", :"#{field}_translated="
|
data/lib/babylonia/version.rb
CHANGED
@@ -235,6 +235,32 @@ describe Babylonia::ClassMethods do
|
|
235
235
|
subject.grasslands(:it).should == 'SOME ITALIAN'
|
236
236
|
end
|
237
237
|
end
|
238
|
+
context "deleting a value" do
|
239
|
+
context "with a string" do
|
240
|
+
it "should be deleted" do
|
241
|
+
subject.grasslands = ''
|
242
|
+
subject.grasslands.should be_nil
|
243
|
+
end
|
244
|
+
end
|
245
|
+
context "with nil" do
|
246
|
+
it "should be deleted" do
|
247
|
+
subject.grasslands = nil
|
248
|
+
subject.grasslands.should be_nil
|
249
|
+
end
|
250
|
+
end
|
251
|
+
context "with a hash containing an empty string" do
|
252
|
+
it "should be deleted" do
|
253
|
+
subject.grasslands = {it: ''}
|
254
|
+
subject.grasslands(:it).should be_nil
|
255
|
+
end
|
256
|
+
end
|
257
|
+
context "with a hash containing nil" do
|
258
|
+
it "should be deleted" do
|
259
|
+
subject.grasslands = {it: nil}
|
260
|
+
subject.grasslands(:it).should be_nil
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
238
264
|
end
|
239
265
|
end
|
240
266
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: babylonia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Beat Richartz
|
@@ -47,10 +47,10 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
49
|
- .rspec
|
50
|
+
- .travis.yml
|
50
51
|
- Gemfile
|
51
52
|
- Gemfile.lock
|
52
53
|
- LICENSE
|
53
|
-
- README.md
|
54
54
|
- README.rdoc
|
55
55
|
- babylonia.gemspec
|
56
56
|
- lib/babylonia.rb
|
data/README.md
DELETED