Skip to content

Commit

Permalink
added Lolita.i18n.connect, Lolita.i18n.reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
gacha committed Nov 21, 2012
1 parent 077a89e commit 13a52df
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -26,4 +26,4 @@ tmp
pkg
spec/test_app/coverage.data
spec/test_app/coverage
spec/tst_app/log
spec/test_app/log/*
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -3,6 +3,7 @@ source "http://rubygems.org"
gemspec

group :test do
gem "debugger"
gem "rails", "~>3.2.2"
gem "cover_me", "~>1.2.0"
gem "bson_ext", "~>1.6.2"
Expand Down
11 changes: 1 addition & 10 deletions lib/lolita-i18n.rb
Expand Up @@ -28,16 +28,7 @@ def i18n
Lolita.configuration.extend(LolitaI18nConfiguration)
Lolita.after_setup do
Lolita.i18n.yaml_backend = ::I18n.backend
Lolita.i18n.include_modules
begin
r = Redis.new
r.ping
::I18n.backend = Lolita.i18n.initialize_chain
rescue Errno::ECONNREFUSED => e
warn "Warning: Lolita was unable to connect to Redis DB: #{e}"
end
true
Lolita.i18n.init
end
Lolita.i18n.load_rails!
Expand Down
37 changes: 36 additions & 1 deletion lib/lolita-i18n/configuration.rb
Expand Up @@ -35,13 +35,48 @@ def backend

# Load translation from yaml.
def load_translations
self.yaml_backend.load_translations
@yaml_backend.load_translations
end

# Create chain where new KeyValue backend is first and Yaml backend is second.
def initialize_chain
::I18n::Backend::Chain.new(self.backend, self.yaml_backend)
end

def init
unless @initialized
include_modules
set_yaml_backend
@initialized = true
connect
end
end

def connect
unless @connected
reconnect
end
end

def reconnect
unless @initialized
init
else
@connected = begin
connection = Redis.new
connection.ping
::I18n.backend = initialize_chain
true
rescue Errno::ECONNREFUSED => e
warn "Warning: Lolita was unable to connect to Redis DB: #{e}"
false
end
end
end

def set_yaml_backend
@yaml_backend ||= ::I18n.backend
end

# Add modules for SimpleBackend that is used for Yaml translations
def include_modules
Expand Down
126 changes: 102 additions & 24 deletions spec/lolita-i18n/configuration_spec.rb
@@ -1,63 +1,141 @@
require 'spec_helper'

describe Lolita::I18n::Configuration do
let(:klass){Lolita::I18n::Configuration}
let(:conf){klass.new}
let(:redis){double("redis")}
subject { described_class.new }
let(:redis){ double("redis", :ping => true) }

it "should load rails if Rails is defined" do
Lolita.stub(:rails3?).and_return(true)
conf.should_receive(:require).with('lolita-i18n/rails')
conf.load_rails!
subject.should_receive(:require).with('lolita-i18n/rails')
subject.load_rails!
end

it "should allow to assign store with Hash as connection options" do
Redis.should_receive(:new).with({:key => "value"}).and_return(redis)
conf.store={:key => "value"}
subject.store = {:key => "value"}
end

it "should allow to assign store as store itself" do
conf.store = redis
conf.store.should eq(redis)
subject.store = redis
subject.store.should eq(redis)
end

it "should return assigned store" do
conf.store = redis
conf.store.should eq(redis)
subject.store = redis
subject.store.should eq(redis)
end

it "should return new Redis connection and warn when no store is assigned" do
Redis.should_receive(:new).and_return(redis)
conf.should_receive(:warn).with("Lolita::I18n No store specified. See Lolita::I18n")
conf.store.should eq(redis)
subject.should_receive(:warn).with("Lolita::I18n No store specified. See Lolita::I18n")
subject.store.should eq(redis)
end

it "should lazy create and return backend" do
Redis.should_receive(:new).and_return(redis)
::I18n::Backend::KeyValue.should_receive(:new).with(redis)
conf.backend
subject.backend
end

it "should load translations" do
yaml_backend = double("yaml_backend")
conf.yaml_backend = yaml_backend
subject.yaml_backend = yaml_backend
yaml_backend.should_receive(:load_translations)
conf.load_translations
subject.load_translations
end

it "should initialize chain" do
conf.yaml_backend = double("yaml backend")
subject.yaml_backend = double("yaml backend")
Redis.stub(:new).and_return(redis)
::I18n::Backend::Chain.should_receive(:new).with(conf.backend,conf.yaml_backend)
conf.initialize_chain
::I18n::Backend::Chain.should_receive(:new).with(subject.backend,subject.yaml_backend)
subject.initialize_chain
Redis.unstub(:new)
end

describe "#connect" do
before(:each){ Redis.stub(:new).and_return(redis) }
after(:each){ Redis.unstub(:new) }

it "include module in ::I18n::Backend::Simple" do
conf.include_modules
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Flatten)
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Pluralization)
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Metadata)
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::InterpolationCompiler)
it "should call reconnect if not connected" do
subject.should_receive(:initialize_chain).once
subject.connect
subject.connect
end

it "should return true when success" do
subject.connect.should be_true
end

it "should return not be true when fail" do
Redis.stub(:new).and_raise(Errno::ECONNREFUSED)
subject.connect.should_not be_true
end
end

describe "#reconnect" do
it "should reconnect" do
subject.should_receive(:initialize_chain).twice
subject.reconnect
subject.reconnect
end

it "should call init if not initialized jet" do
subject.should_receive(:init).once
subject.reconnect
end

it "should not call init if already initialized" do
subject.reconnect
subject.should_not_receive(:init)
subject.reconnect
end

it "should return true when success" do
subject.reconnect.should be_true
end

it "should return false when fail" do
Redis.stub(:new).and_raise(Errno::ECONNREFUSED)
subject.reconnect.should_not be_true
Redis.unstub(:new)
end

it "should initialize chain" do
chain = double("chain")
subject.should_receive(:initialize_chain).and_return(chain)
redis = double("redis", :ping => true)
Redis.stub(:new).and_return(redis)
::I18n.should_receive(:backend=).with(chain)
subject.reconnect
Redis.unstub(:new)
end
end

describe "#init" do
it "should call all propper methods" do
subject.should_receive(:include_modules)
subject.should_receive(:set_yaml_backend)
subject.should_receive(:connect)
subject.init
end

it "should run only once" do
subject.should_receive(:include_modules).once
subject.should_receive(:set_yaml_backend).once
subject.should_receive(:connect).once
subject.init
subject.init
end
end

describe "#include_modules" do
it "include module in ::I18n::Backend::Simple" do
subject.include_modules
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Flatten)
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Pluralization)
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Metadata)
::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::InterpolationCompiler)
end
end

end
21 changes: 2 additions & 19 deletions spec/lolita_i18n_spec.rb
Expand Up @@ -2,26 +2,9 @@

describe Lolita::I18n do
describe "loaded" do

it "should check Redis connection after Lolita.setup" do
Lolita.i18n.should_receive(:yaml_backend=).and_return(double())
Lolita.i18n.should_receive(:include_modules).and_return(true)
chain = double("chain")
Lolita.i18n.should_receive(:initialize_chain).and_return(chain)
redis = double("redis")
Redis.stub(:new).and_return(redis)
redis.stub(:ping => true)
::I18n.should_receive(:backend=).with(chain)

Lolita.setup{}
end

it "should warn when Redis is not available" do
Lolita.i18n.should_receive(:yaml_backend=).and_return(double())
Lolita.i18n.should_receive(:include_modules).and_return(true)
redis = double("redis")
Redis.stub(:new).and_return(redis)
redis.stub(:ping).and_raise(Errno::ECONNREFUSED)

Lolita.i18n.should_receive(:init)
Lolita.setup{}
end

Expand Down

0 comments on commit 13a52df

Please sign in to comment.