babylonia 0.1.0 → 0.1.1

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: 471b84c7ea7c5f3e6a22c69a64b74438b159480c
4
- data.tar.gz: 3895c1969ff6a922574c66123a819211e849f60f
3
+ metadata.gz: e5e07171440e9ebac6b51d3f768b68471dcbd5cd
4
+ data.tar.gz: 7c0141cd5353cfe4e8e4fa10abdad68675388466
5
5
  SHA512:
6
- metadata.gz: 960ef518e49a6a76ab9fd5ac1a1016123b9b3a10cba86c2fb646f7ad2cdd03d52b55cdd1ad474c5b36c2188efeead630c238bf21ca42135cd1bc73538c9f8090
7
- data.tar.gz: 1a0d5661cc64f338b0d83fac5bf1d8f370e4c33a526c65c749cd5a59f64262d3e168626aea25af7c7c13d98c3f609c921ab473acba4168553d9eb3722ae9925f
6
+ metadata.gz: bc939cba85b5aadb2bf909dcccafff796137489ee22956aaf91c000f347e21b3fc3db967f2736c871b78848f56b8f4b77ec65af1cc2c172da6548a056952b30d
7
+ data.tar.gz: df443e957bc998fb312d3f483f9fb38c68e8bfd817abf897db36f200ea7804811268cf1655a2b38a8f6508aefb76b836caa1e384c1d873120f3ec266bddab4e9
@@ -30,14 +30,15 @@ module Babylonia
30
30
  fields.each do |field|
31
31
  # Alias method chain the field to a translated value
32
32
  # @param [Symbol] locale Pass a locale to get the field translation in this specific locale
33
+ # @param [Boolean] fallback Whether a fallback should be used, defaults to true
33
34
  # @return [String, NilClass] Either the string with the translation, the fallback, the placeholder or nil
34
35
  # @example Call a field in italian
35
36
  # your_instance.field(:it) #=> "Translation"
36
37
  #
37
- define_method :"#{field}_translated" do |l=nil|
38
+ define_method :"#{field}_translated" do |l=nil, options={fallback: true}|
38
39
  field_hash = send(:"#{field}_hash")
39
40
  translation = field_hash[l || locale]
40
- translation = field_hash[default_locale] if translation.nil? or translation.empty? and locale_fallback?
41
+ translation = field_hash[default_locale] if translation.nil? or translation.empty? and options[:fallback] and locale_fallback?
41
42
  (translation.nil? or translation.empty?) ? missing_translation_placeholder(field) : translation
42
43
  end
43
44
  alias_method :"#{field}_raw", field
@@ -141,9 +142,20 @@ module Babylonia
141
142
  end
142
143
 
143
144
  # Define method missing to be able to access a language directly
145
+ # Enables to call a language virtual attribute directly
146
+ # @note Since the virtual attribute is called directly, there is no fallback on this unless you set it to true
147
+ # @example Call a getter directly
148
+ # object.field_de #=> 'DEUTSCH'
149
+ # @example Call a setter directly
150
+ # object.field_de = 'DEUTSCH'
151
+ # @example Call an untranslated field
152
+ # object.field_it #=> nil
153
+ #
154
+ # @example Call a field with fallback
155
+ # object.field_it(fallback: true)
144
156
  define_method :method_missing do |meth, *args, &block|
145
157
  if (m = meth.to_s.match(/\A([^_]+)_(\w+)(=)?\z/).to_a[1..3]) && localized?(m[0]) && has_available_locale?(m[1])
146
- send(m[0] + m[2].to_s, m[2] ? {m[1].to_sym => args.first} : m[1].to_sym)
158
+ m[2] ? send(m[0] + m[2].to_s, {m[1].to_sym => args.first}) : send(m[0], m[1].to_sym, args.first || {})
147
159
  else
148
160
  super(meth, *args, &block)
149
161
  end
@@ -1,5 +1,5 @@
1
1
  module Babylonia
2
2
  # The current gem version
3
3
  #
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
@@ -32,6 +32,11 @@ describe Babylonia::ClassMethods do
32
32
  it "should return the translation in that locale" do
33
33
  subject.marshes(:de).should == 'FALLBACK'
34
34
  end
35
+ context "with fallback to false" do
36
+ it "should return nil" do
37
+ subject.marshes(:it, fallback: false).should be_nil
38
+ end
39
+ end
35
40
  end
36
41
  end
37
42
  context "with only fallback data" do
@@ -66,10 +71,17 @@ describe Babylonia::ClassMethods do
66
71
  context "with the missing method matching the pattern FIELD_LANGUAGE" do
67
72
  let(:meth) { :marshes_en }
68
73
  it "should call the attribute method with an argument" do
69
- subject.should_receive(:marshes).with(:en).once
74
+ subject.should_receive(:marshes).with(:en, {}).once
70
75
  subject.send(meth)
71
76
  end
72
77
  end
78
+ context "with the missing method matching the pattern FIELD_LANGUAGE and a fallback argument" do
79
+ let(:meth) { :marshes_en }
80
+ it "should call the attribute method with the fallback argument" do
81
+ subject.should_receive(:marshes).with(:en, {fallback: true}).once
82
+ subject.send(meth, fallback: true)
83
+ end
84
+ end
73
85
  context "with the missing method not matching the pattern" do
74
86
  let(:meth) { :marshes_something_else_entirely }
75
87
  it "should raise Method Missing" do
@@ -270,6 +282,11 @@ describe Babylonia::ClassMethods do
270
282
  it "should return the translation in that locale" do
271
283
  subject.grasslands(:de).should == 'FALLBACK'
272
284
  end
285
+ context "with fallback to false" do
286
+ it "should return the placeholder" do
287
+ subject.grasslands(:gb, fallback: false).should == "<span class='missing translation'>Translation missing for grasslands</span>"
288
+ end
289
+ end
273
290
  end
274
291
  end
275
292
  context "with only fallback data" do
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Beat Richartz