Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Store.put! to overwrite existing design documents, if any.
  • Loading branch information
eee-c committed Jul 13, 2009
1 parent 93ece90 commit 2c5f14a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
6 changes: 3 additions & 3 deletions couch_design_docs.gemspec
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion lib/couch_design_docs.rb
Expand Up @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions lib/couch_design_docs/store.rb
@@ -1,4 +1,4 @@
require 'rest_client'
require 'restclient'
require 'json'

module CouchDesignDocs
Expand All @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions spec/couch_design_docs_spec.rb
Expand Up @@ -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).
Expand Down

0 comments on commit 2c5f14a

Please sign in to comment.