class Tatami::Couch
Attributes
Public Class Methods
@option opts [Boolean] :log @option opts [String] :db
# File lib/tatami/couch.rb, line 27 def initialize db, url='http://localhost:5984', opts={} @env = opts.delete(:env) @db, @url, @opts = db, url, opts end
Public Instance Methods
delete and recreate the database
# File lib/tatami/couch.rb, line 14 def blank! conn.delete "/#{db_env}" conn.put "/#{db_env}" self end
# File lib/tatami/couch.rb, line 209 def bulk_import limit=100, &b BulkImport.new self, limit, &b end
# File lib/tatami/couch.rb, line 38 def cast_response data, format case format when :string data when :object JSON.parse(data) else Map.new JSON.parse(data) end end
# File lib/tatami/couch.rb, line 6 def conn @conn ||= Faraday.new(:url => @url) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :logger if @opts[:log] faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end
# File lib/tatami/couch.rb, line 21 def db_env() @env ? "#{@db}_#{@env}" : @db end
# File lib/tatami/couch.rb, line 74 def db_info() get_db "" end
creates new type in _design/ document @param [Symbol] type :view, :list, :show
# File lib/tatami/couch.rb, line 173 def design_new type, name, content doc_merge_cached "_design/#{db_env}", :language => 'coffeescript', "#{type}s" => { name => content } end
_id 0c8e32db863b87c9f6233d5728545922 _rev 1-08c111cb2ccfbf117ed931bef444f730 error: not_found reason: missing name Frank GET /db/doc
# File lib/tatami/couch.rb, line 142 def doc id r = get_db "/#{id}" r[:error] == "not_found" ? nil : r end
@param [String] file_root
# File lib/tatami/couch.rb, line 113 def doc_attach_app id, file_root # curl -X PUT -v http://127.0.0.1:5984/cars/flot/index.html --data-binary "@test/fixtures/index.html" --header "Accept: text/html" --header "Transfer-Encoding: chunked" --header "Expect:" require 'tatami/app' put_db "/#{id}", "_attachments" => App.new.attachments(file_root) end
# File lib/tatami/couch.rb, line 119 def doc_create_or_update id, attributes if old = doc(id) doc_update old, attributes else doc_new id, attributes end end
# File lib/tatami/couch.rb, line 200 def doc_merge id, attributes old = doc id doc_new old[:_id], old.combine(attributes) end
# File lib/tatami/couch.rb, line 161 def doc_merge_cached id, attributes @cache ||= Map.new @cache[id] ||= begin doc(id) || Map.new end @cache.combine! id => attributes r = doc_new id, @cache[id] @cache[id][:_rev] = r[:rev] end
PUT /db/doc @return [Doc] ok true id 0c8e32db863b87c9f6233d572854d6db rev 1-08c111cb2ccfbf117ed931bef444f730
# File lib/tatami/couch.rb, line 107 def doc_new id, attributes id ||= next_id put_db "/#{id}", attributes end
# File lib/tatami/couch.rb, line 205 def doc_new_bulk documents post_db "/_bulk_docs", {:docs => documents} end
@param [Hash] old @param [Hash] new
# File lib/tatami/couch.rb, line 156 def doc_update old, new attributes = new.merge(:_rev => old[:_rev]) doc_new old[:_id], attributes end
# File lib/tatami/couch.rb, line 32 def get path, opts={} puts "GET #{path}" if opts[:log] data = conn.get(path).body cast_response data, opts[:format] end
# File lib/tatami/couch.rb, line 53 def get_db path, opts={} get "/#{db_env}#{path}", opts end
# File lib/tatami/couch.rb, line 49 def get_db_string path conn.get("/#{db_env}#{path}").body end
views name map views name reduce lists name
# File lib/tatami/couch.rb, line 182 def load_design pattern="*" [ %w[erlang %% erl], %w[coffeescript # coffee] ].each do |language, comment, ext | Dir.glob("couches/#{@db}/#{pattern}.#{ext}").each do |path| source = IO.read(path) name = File.basename(path)[/[^\.]+/] map = Map.new(language: language) source.split(/^(?=#{comment} (?:views|lists) \S+)/m).each do |element| element.split("\n").first =~ /#{comment} (\S+) (\S+) ?(map|reduce)?/ values = [$1,$2,$3].compact << element map.set *values end doc_create_or_update "_design/#{name}", map end end end
@return [String] autogenerated available id (caches 10)
# File lib/tatami/couch.rb, line 128 def next_id @next_ids ||= [] if @next_ids.empty? @next_ids = get("_uuids?count=10")[:uuids] end @next_ids.pop end
# File lib/tatami/couch.rb, line 63 def post_db url, body, opts={} verb :post, "/#{db_env}#{url}", body, opts end
# File lib/tatami/couch.rb, line 69 def post_db_string url, body conn.post("/#{db_env}#{url}", JSON.generate(body), 'Content-Type' => 'application/json').body end
# File lib/tatami/couch.rb, line 67 def put_db(url, body) verb :put, "/#{db_env}#{url}", body end
GET /db/_design/design-doc/_show/show-name GET /db/_design/design-doc/_show/show-name/doc
# File lib/tatami/couch.rb, line 149 def show name, doc=nil, design=db_env d = doc ? "/#{doc}" : "" get_db_string "/_design/#{design}/_show/#{name}#{d}" end
# File lib/tatami/couch.rb, line 57 def verb method, url, body, opts={} puts "#{method.upcase} #{url} #{body.inspect}" if opts[:log] resp = conn.send method, url, JSON.generate(body), 'Content-Type' => 'application/json' cast_response resp.body, opts[:format] end
# File lib/tatami/couch.rb, line 73 def version() get('/')[:version] end
GET /db/_design/design-doc/_view/view-name
# File lib/tatami/couch.rb, line 232 def view name @views ||= Map.new @views[name] ||= View.new self, name.to_s end
# File lib/tatami/couch.rb, line 96 def with_documents! *docs blank! load_design doc_new_bulk docs end
db_name: cars doc_count: 0 doc_del_count: 0 update_seq: 0 purge_seq: 0 compact_running: false disk_size: 79 data_size: 0 instance_start_time: '1364126614255010' disk_format_version: 6 committed_update_seq: 0
# File lib/tatami/couch.rb, line 87 def with_fixtures! names, *docs blank! load_design documents = docs.map do |values| Hash[ names.zip(values) ] end doc_new_bulk documents end