lib/couch/actions/push.rb in couch-0.1.2 vs lib/couch/actions/push.rb in couch-0.2.0
- old
+ new
@@ -1,32 +1,47 @@
require 'couch/actions/base'
require 'couch/design_document'
+require 'pathname'
require "rest_client"
module Couch
module Actions
class Push < Base
+ def initialize(*args)
+ super
+ @doc = DesignDocument.new
+ end
+
def create_database_unless_exists
- RestClient.put DesignDocument.database, nil
- say "Created database %s" % DesignDocument.database
+ RestClient.put @doc.database, nil
+ say "Created database %s" % @doc.database
rescue RestClient::PreconditionFailed
end
def push
- doc = DesignDocument.build_from_filesystem(destination_root)
- say "Pushing to %s" % DesignDocument.url
+ root = Pathname.new(destination_root)
+ filenames = Dir[root.join "**/*"]
+ filenames.map! { |file| Pathname.new file }
+ filenames.map! { |path| path.relative_path_from root }
+ filenames.delete_if { |path| !path.file? }
+ filenames.map!(&:to_s)
+ @doc.read(filenames) do |filename|
+ File.read File.join(destination_root, filename)
+ end
- resp = RestClient.put DesignDocument.url, doc.to_json
+ say "Pushing to %s" % @doc.url
+
+ resp = RestClient.put @doc.url, @doc.json
response = JSON.parse(resp.body)
if response["ok"]
- rev = response["rev"]
- File.open File.join(destination_root, "_rev.js"), "w" do |file|
- file << "#{rev}\n"
+ @doc.rev = response["rev"]
+ File.open File.join(destination_root, "_rev"), "w" do |file|
+ file << @doc.rev
end
- say "Pushed %s" % rev
+ say "Pushed %s" % @doc.rev
else
say "Error occured: %s" % response.inspect
end
rescue RestClient::Conflict