Class: CouchFoo::DatabaseWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_foo/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, bulk_save_default, *args) ⇒ DatabaseWrapper

Returns a new instance of DatabaseWrapper.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/couch_foo/database.rb', line 9

def initialize(options, bulk_save_default, *args)
  self.database = CouchRest.database(options[:host] + "/" + options[:database])
  self.bulk_save_default = bulk_save_default
  
  # Check database ok
  begin
    self.database_version = (JSON.parse(RestClient.get(options[:host]))["version"]).gsub(/-.+/,"").to_f
  rescue Exception => e
    if e.is_a?(Errno::ECONNREFUSED)
      raise CouchFooError, "CouchDB not started"
    else
      raise CouchFooError, "Error determining CouchDB version"
    end
  end
  
  # Due to CouchDB view API changes in 0.9 and CouchREST only supporting newer version
  if version > 0.8 && CouchRest::VERSION.to_f < 0.21
    raise CouchFooError, "CouchFoo requires CouchRest > 0.2 for use with CouchDB 0.9"
  elsif version < 0.9 && (CouchRest::VERSION.to_f >= 0.21 || CouchRest::VERSION.to_f <= 0.15)
    raise CouchFooError, "CouchFoo requires 0.15 < CouchRest < 0.21 for use with CouchDB 0.8"
  end
end

Instance Attribute Details

#bulk_save_defaultObject

Returns the value of attribute bulk_save_default.



7
8
9
# File 'lib/couch_foo/database.rb', line 7

def bulk_save_default
  @bulk_save_default
end

#databaseObject

Returns the value of attribute database.



7
8
9
# File 'lib/couch_foo/database.rb', line 7

def database
  @database
end

#database_versionObject

Returns the value of attribute database_version.



7
8
9
# File 'lib/couch_foo/database.rb', line 7

def database_version
  @database_version
end

Instance Method Details

#bulk_save?Boolean

Returns:



89
90
91
# File 'lib/couch_foo/database.rb', line 89

def bulk_save?
  bulk_save_default
end

#commitObject



50
51
52
53
54
55
56
57
# File 'lib/couch_foo/database.rb', line 50

def commit
  begin
    response = database.bulk_save
    check_response_ok(response)
  rescue Exception => e
    handle_exception(e)
  end
end

#delete(doc) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/couch_foo/database.rb', line 41

def delete(doc)
  begin
    response = database.delete(doc)
    check_response_ok(response)
  rescue Exception => e
    handle_exception(e)
  end
end

#get(doc) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/couch_foo/database.rb', line 59

def get(doc)
  begin
    database.get(doc)
  rescue Exception => e
    handle_exception(e)
  end
end

#save(doc, bulk_save = bulk_save?) ) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/couch_foo/database.rb', line 32

def save(doc, bulk_save = bulk_save?)
  begin
    response = database.save_doc(doc, bulk_save)
    check_response_ok(response)
  rescue Exception => e
    handle_exception(e)
  end
end

#slow_view(doc, params) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/couch_foo/database.rb', line 75

def slow_view(doc, params)
  begin
    database.slow_view(doc, params)
  rescue Exception => e
    handle_exception(e)
  end
end

#transaction(&block) ⇒ Object

At the moment this is limited by the CouchREST bulk save limit of 50 transactions



84
85
86
87
# File 'lib/couch_foo/database.rb', line 84

def transaction(&block)
  yield
  commit
end

#versionObject



93
94
95
# File 'lib/couch_foo/database.rb', line 93

def version
  database_version
end

#view(doc, params) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/couch_foo/database.rb', line 67

def view(doc, params)
  begin
    database.view(doc, params)
  rescue Exception => e
    handle_exception(e)
  end
end