Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensuring that with_locale changes locale back even on error, using Ar…

…ray#reverse_each for fallback iteration instead of complicated down_to logic
  • Loading branch information...
commit e57ee5f4311eed31ff052557d3bea87d936f230e 1 parent 1750319
Cameron Dutro authored camertron committed
Showing with 20 additions and 4 deletions.
  1. +5 −4 lib/twitter_cldr.rb
  2. +15 −0 spec/twitter_cldr_spec.rb
View
9 lib/twitter_cldr.rb
@@ -75,6 +75,7 @@ def with_locale(locale)
old_locale = @locale
@locale = locale
result = yield
+ ensure
@locale = old_locale
result
end
@@ -120,15 +121,15 @@ def supported_locale?(locale)
protected
def find_fallback
- (locale_fallbacks.size - 1).downto(0).each do |i|
- result = if locale_fallbacks[i].is_a?(Proc)
+ locale_fallbacks.reverse_each do |fallback|
+ result = if fallback.is_a?(Proc)
begin
- locale_fallbacks[i].call
+ fallback.call
rescue
nil
end
else
- locale_fallbacks[i]
+ fallback
end
return result if result
end
View
15 spec/twitter_cldr_spec.rb
@@ -168,6 +168,21 @@
TwitterCldr.with_locale(:es) { TwitterCldr::Shared::Languages.from_code(:es) }.should match_normalized("español")
TwitterCldr::Shared::Languages.from_code(:es).should == "Spanish"
end
+
+ it "switches the locale back to the original if the block raises an error" do
+ TwitterCldr.locale.should == :en
+ locale_inside_block = nil
+
+ lambda do
+ TwitterCldr.with_locale(:es) do
+ locale_inside_block = TwitterCldr.locale
+ raise "Error!"
+ end
+ end.should raise_error
+
+ locale_inside_block.should == :es
+ TwitterCldr.locale.should == :en
+ end
end
describe '#resources' do
Please sign in to comment.
Something went wrong with that request. Please try again.