Module: CouchRest

Defined in:
lib/couchrest.rb,
lib/couchrest/core/model.rb,
lib/couchrest/core/server.rb,
lib/couchrest/helper/pager.rb,
lib/couchrest/commands/push.rb,
lib/couchrest/core/database.rb,
lib/couchrest/helper/streamer.rb,
lib/couchrest/commands/generate.rb,
lib/couchrest/helper/file_manager.rb

Overview

CouchRest::Model - ORM, the CouchDB way

Defined Under Namespace

Modules: Commands, Model Classes: Database, FileManager, Pager, Server, Streamer

Class Method Summary collapse

Class Method Details

.database(url) ⇒ Object



87
88
89
90
91
# File 'lib/couchrest.rb', line 87

def database url
  parsed = parse url
  cr = CouchRest.new(parsed[:host])
  cr.database(parsed[:database])
end

.database!(url) ⇒ Object

ensure that a database exists creates it if it isn’t already there returns it after it’s been created



81
82
83
84
85
# File 'lib/couchrest.rb', line 81

def database! url
  parsed = parse url
  cr = CouchRest.new(parsed[:host])
  cr.database!(parsed[:database])
end

.delete(uri) ⇒ Object



107
108
109
# File 'lib/couchrest.rb', line 107

def delete uri
  JSON.parse(RestClient.delete(uri))
end

.get(uri) ⇒ Object



98
99
100
# File 'lib/couchrest.rb', line 98

def get uri
  JSON.parse(RestClient.get(uri), :max_nesting => false)
end

.new(*opts) ⇒ Object

todo, make this parse the url and instantiate a Server or Database instance depending on the specificity.



43
44
45
# File 'lib/couchrest.rb', line 43

def new(*opts)
  Server.new(*opts)
end

.paramify_url(url, params = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/couchrest.rb', line 111

def paramify_url url, params = nil
  if params
    query = params.collect do |k,v|
      v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
      "#{k}=#{CGI.escape(v.to_s)}"
    end.join("&")
    url = "#{url}?#{query}"
  end
  url
end

.parse(url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/couchrest.rb', line 47

def parse url
  case url
  when /^http:\/\/(.*)\/(.*)\/(.*)/
    host = $1
    db = $2
    docid = $3
  when /^http:\/\/(.*)\/(.*)/
    host = $1
    db = $2
  when /^http:\/\/(.*)/
    host = $1
  when /(.*)\/(.*)\/(.*)/
    host = $1
    db = $2
    docid = $3
  when /(.*)\/(.*)/
    host = $1
    db = $2
  else
    db = url
  end

  db = nil if db && db.empty?

  {
    :host => host || "localhost:5984",
    :database => db,
    :doc => docid
  }
end

.post(uri, doc = nil) ⇒ Object



102
103
104
105
# File 'lib/couchrest.rb', line 102

def post uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.post(uri, payload))
end

.put(uri, doc = nil) ⇒ Object



93
94
95
96
# File 'lib/couchrest.rb', line 93

def put uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.put(uri, payload))
end