Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add export to Copycopter cache

  • Loading branch information...
commit af52b01f8f50c28185511be5126cf9dbae30c53e 1 parent 6416897
@chrishunt chrishunt authored
View
6 lib/copycopter_client.rb
@@ -24,6 +24,12 @@ def self.deploy
client.deploy
end
+ # Issues a new export, returning yaml representation of blurb cache.
+ # This is called when the copycopter:export rake task is invoked.
+ def self.export
+ cache.export
+ end
+
# Starts the polling process.
def self.start_poller
poller.start
View
21 lib/copycopter_client/cache.rb
@@ -43,6 +43,27 @@ def keys
lock { @blurbs.keys }
end
+ # Yaml representation of all blurbs
+ # @return [String] yaml
+ def export
+ keys = {}
+ lock do
+ @blurbs.each do |blurb_key, value|
+ current = keys
+ yaml_keys = blurb_key.split('.')
+
+ 0.upto(yaml_keys.size - 2) do |i|
+ key = yaml_keys[i]
+ current[key] ||= {}
+ current = current[key]
+ end
+
+ current[yaml_keys.last] = value
+ end
+ end
+ keys.to_yaml unless keys.size < 1
+ end
+
# Waits until the first download has finished.
def wait_for_download
if pending?
View
75 spec/copycopter_client/cache_spec.rb
@@ -205,5 +205,80 @@ def violated(failure)
cache.should have_received(:flush)
end
+
+ describe "#export" do
+ before do
+ save_blurbs
+ @cache = build_cache
+ @cache.download
+ end
+
+ let(:save_blurbs) {}
+
+ it "Can be invoked from the top-level constant" do
+ CopycopterClient.configure do |config|
+ config.cache = @cache
+ end
+ @cache.stubs(:export)
+
+ CopycopterClient.export
+
+ @cache.should have_received(:export)
+ end
+
+ context "with no blurb keys" do
+ it "returns no yaml" do
+ @cache.export.should == nil
+ end
+ end
+
+ context "with single-level blurb keys" do
+ let(:save_blurbs) do
+ client['key'] = 'test value'
+ client['other_key'] = 'other test value'
+ end
+
+ it "returns blurbs as yaml" do
+ @cache.export.should ==
+ "---" + "\n" +
+ "key: test value" + "\n" +
+ "other_key: other test value" + "\n"
+ end
+ end
+
+ context "with multi-level blurb keys" do
+ let(:save_blurbs) do
+ client['en.test.key'] = 'en test value'
+ client['en.test.other_key'] = 'en other test value'
+ client['fr.test.key'] = 'fr test value'
+ end
+
+ it "returns blurbs as yaml" do
+ @cache.export.should ==
+ "---" + "\n" +
+ "en:" + "\n" +
+ " test:" + "\n" +
+ " key: en test value" + "\n" +
+ " other_key: en other test value" + "\n" +
+ "fr:" + "\n" +
+ " test:" + "\n" +
+ " key: fr test value" + "\n"
+ end
+ end
+
+ context "with conflicting blurb keys" do
+ let(:save_blurbs) do
+ client['en.test.key'] = 'test value'
+ client['en.test'] = 'other test value'
+ end
+
+ it "retains the new key" do
+ @cache.export.should ==
+ "---" + "\n" +
+ "en:" + "\n" +
+ " test: other test value" + "\n"
+ end
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.