From 2c5f14a78aac05647387ce554c82b2c8a1dba877 Mon Sep 17 00:00:00 2001 From: eee-c Date: Sun, 12 Jul 2009 22:36:08 -0400 Subject: [PATCH] Add Store.put! to overwrite existing design documents, if any. --- couch_design_docs.gemspec | 6 +++--- lib/couch_design_docs.rb | 2 +- lib/couch_design_docs/store.rb | 17 +++++++++++++++-- spec/couch_design_docs_spec.rb | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/couch_design_docs.gemspec b/couch_design_docs.gemspec index b116cdf..66555b0 100644 --- a/couch_design_docs.gemspec +++ b/couch_design_docs.gemspec @@ -2,17 +2,17 @@ Gem::Specification.new do |s| s.name = %q{couch_design_docs} - s.version = "1.0.0" + s.version = "1.0.1" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Chris Strom"] - s.date = %q{2009-07-11} + s.date = %q{2009-07-12} s.default_executable = %q{couch_design_docs} s.description = %q{Manage CouchDB views.} s.email = %q{chris@eeecooks.com} s.executables = ["couch_design_docs"] s.extra_rdoc_files = ["History.txt", "README.txt", "bin/couch_design_docs"] - s.files = ["History.txt", "README.txt", "Rakefile", "bin/couch_design_docs", "fixtures/a/b/c.js", "fixtures/a/b/d.js", "lib/couch_design_docs.rb", "lib/couch_design_docs/directory.rb", "lib/couch_design_docs/store.rb", "spec/couch_design_docs_spec.rb", "spec/spec_helper.rb", "test/test_couch_design_docs.rb"] + s.files = ["History.txt", "README.txt", "Rakefile", "bin/couch_design_docs", "couch_design_docs.gemspec", "fixtures/a/b/c.js", "fixtures/a/b/d.js", "lib/couch_design_docs.rb", "lib/couch_design_docs/directory.rb", "lib/couch_design_docs/store.rb", "spec/couch_design_docs_spec.rb", "spec/spec_helper.rb", "test/test_couch_design_docs.rb"] s.has_rdoc = true s.homepage = %q{http://github.com/eee-c/couch_design_docs} s.rdoc_options = ["--main", "README.txt"] diff --git a/lib/couch_design_docs.rb b/lib/couch_design_docs.rb index 261feef..c7694fc 100644 --- a/lib/couch_design_docs.rb +++ b/lib/couch_design_docs.rb @@ -2,7 +2,7 @@ module CouchDesignDocs # :stopdoc: - VERSION = '1.0.0' + VERSION = '1.0.1' LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR # :startdoc: diff --git a/lib/couch_design_docs/store.rb b/lib/couch_design_docs/store.rb index 1191191..b8dbdb2 100644 --- a/lib/couch_design_docs/store.rb +++ b/lib/couch_design_docs/store.rb @@ -1,4 +1,4 @@ -require 'rest_client' +require 'restclient' require 'json' module CouchDesignDocs @@ -11,10 +11,23 @@ def initialize(url) def load(h) h.each_pair do |document_name, doc| - Store.put("#{url}/_design/#{document_name}", doc) + Store.put!("#{url}/_design/#{document_name}", doc) end end + # Create or replace the document located at "path" with the + # Hash document "doc" + def self.put!(path, doc) + self.put(path, doc) + rescue RestClient::RequestFailed + self.delete_and_put(path, doc) + end + + def self.delete_and_put(path, doc) + self.delete(path) + self.put(path, doc) + end + def self.put(path, doc) RestClient.put path, doc.to_json, diff --git a/spec/couch_design_docs_spec.rb b/spec/couch_design_docs_spec.rb index 581fb05..895a8c8 100644 --- a/spec/couch_design_docs_spec.rb +++ b/spec/couch_design_docs_spec.rb @@ -22,6 +22,38 @@ } end + it "should be able to put a new document" do + Store. + should_receive(:put). + with("uri", { }) + + Store.put!("uri", { }) + end + + it "should delete existing docs if first put fails" do + Store. + stub!(:put). + and_raise(RestClient::RequestFailed) + + Store. + should_receive(:delete_and_put). + with("uri", { }) + + Store.put!("uri", { }) + end + + it "should be able to delete and put" do + Store. + should_receive(:delete). + with("uri") + + Store. + should_receive(:put). + with("uri", { }) + + Store.delete_and_put("uri", { }) + end + it "should be able to load a hash into design docs" do RestClient. should_receive(:put).