Class: RelaxDB::CouchDB

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CouchDB

Returns a new instance of CouchDB.



73
74
75
76
77
# File 'lib/relaxdb/server.rb', line 73

def initialize(config)
  @get_count, @put_count = 0, 0
  @server = RelaxDB::Server.new(config[:host], config[:port])
  @logger = config[:logger] ? config[:logger] : Logger.new(Tempfile.new('couchdb.log'))
end

Instance Attribute Details

#get_countObject

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



71
72
73
# File 'lib/relaxdb/server.rb', line 71

def get_count
  @get_count
end

#loggerObject (readonly)

Returns the value of attribute logger.



67
68
69
# File 'lib/relaxdb/server.rb', line 67

def logger
  @logger
end

#put_countObject

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



71
72
73
# File 'lib/relaxdb/server.rb', line 71

def put_count
  @put_count
end

Instance Method Details

#db_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/relaxdb/server.rb', line 84

def db_exists?(name)
  @server.get("/#{name}") rescue false
end

#delete(path = nil) ⇒ Object



104
105
106
107
# File 'lib/relaxdb/server.rb', line 104

def delete(path=nil)
  @logger.info("DELETE /#{@db}/#{unesc(path)}")
  @server.delete("/#{@db}/#{path}")
end

#delete_db(name) ⇒ Object



88
89
90
91
# File 'lib/relaxdb/server.rb', line 88

def delete_db(name)
  @logger.info("Deleting database #{name}")
  @server.delete("/#{name}")
end

#get(path = nil, *ignored) ⇒ Object

*ignored allows methods to invoke get or post indifferently via send



110
111
112
113
114
# File 'lib/relaxdb/server.rb', line 110

def get(path=nil, *ignored)
  @get_count += 1
  @logger.info("GET /#{@db}/#{unesc(path)}")
  @server.get("/#{@db}/#{path}")
end

#list_dbsObject



93
94
95
# File 'lib/relaxdb/server.rb', line 93

def list_dbs
  JSON.parse(@server.get("/_all_dbs").body)
end

#nameObject



136
137
138
# File 'lib/relaxdb/server.rb', line 136

def name
  @db
end

#name=(name) ⇒ Object



140
141
142
# File 'lib/relaxdb/server.rb', line 140

def name=(name)
  @db = name
end

#post(path = nil, json = nil) ⇒ Object



116
117
118
119
# File 'lib/relaxdb/server.rb', line 116

def post(path=nil, json=nil)
  @logger.info("POST /#{@db}/#{unesc(path)} #{json}")
  @server.post("/#{@db}/#{path}", json)
end

#put(path = nil, json = nil) ⇒ Object



121
122
123
124
125
# File 'lib/relaxdb/server.rb', line 121

def put(path=nil, json=nil)
  @put_count += 1
  @logger.info("PUT /#{@db}/#{unesc(path)} #{json}")
  @server.put("/#{@db}/#{path}", json)
end

#replicate_db(source, target) ⇒ Object



97
98
99
100
101
102
# File 'lib/relaxdb/server.rb', line 97

def replicate_db(source, target)
  @logger.info("Replicating from #{source} to #{target}")
  create_db_if_non_existant target      
  data = { "source" => source, "target" => target}
  @server.post("/_replicate", data.to_json)
end

#unesc(path) ⇒ Object



127
128
129
130
# File 'lib/relaxdb/server.rb', line 127

def unesc(path)
  # path
  path ? ::CGI::unescape(path) : ""
end

#uriObject



132
133
134
# File 'lib/relaxdb/server.rb', line 132

def uri
  "#@server" / @db
end

#use_db(name) ⇒ Object



79
80
81
82
# File 'lib/relaxdb/server.rb', line 79

def use_db(name)
  create_db_if_non_existant(name)
  @db = name
end