Class: CouchPotato::Database

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

Defined Under Namespace

Classes: ValidationsFailedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(couchrest_database, name: nil) ⇒ Database

Returns a new instance of Database.



11
12
13
14
# File 'lib/couch_potato/database.rb', line 11

def initialize(couchrest_database, name: nil)
  @name = name
  @couchrest_database = couchrest_database
end

Instance Attribute Details

#cacheObject

Pass in a cache to enable caching #load calls. A cache needs to respond to #[], #[]= and #clear (just use a Hash).



6
7
8
# File 'lib/couch_potato/database.rb', line 6

def cache
  @cache
end

#nameObject (readonly)

the (unresolved) name of the database unless this is the default database



9
10
11
# File 'lib/couch_potato/database.rb', line 9

def name
  @name
end

Instance Method Details

#couchrest_databaseObject

returns the underlying CouchRest::Database instance



146
147
148
# File 'lib/couch_potato/database.rb', line 146

def couchrest_database
  @couchrest_database
end

#destroy_document(document) ⇒ Object Also known as: destroy



98
99
100
101
102
103
104
105
# File 'lib/couch_potato/database.rb', line 98

def destroy_document(document)
  cache&.clear
  begin
    destroy_document_without_conflict_handling document
  rescue CouchRest::Conflict
    retry if document = document.reload
  end
end

#first(spec) ⇒ Object

returns the first result from a #view query or nil



63
64
65
66
# File 'lib/couch_potato/database.rb', line 63

def first(spec)
  spec.view_parameters = spec.view_parameters.merge({:limit => 1})
  view(spec).first
end

#first!(spec) ⇒ Object

returns th first result from a #view or raises CouchPotato::NotFound



69
70
71
# File 'lib/couch_potato/database.rb', line 69

def first!(spec)
  first(spec) || raise(CouchPotato::NotFound)
end

#inspectObject

:nodoc:



141
142
143
# File 'lib/couch_potato/database.rb', line 141

def inspect #:nodoc:
  "#<CouchPotato::Database @root=\"#{couchrest_database.root}\">"
end

#load!(id) ⇒ Object

loads one or more documents by its id(s) behaves like #load except it raises a CouchPotato::NotFound if any of the documents could not be found



132
133
134
135
136
137
138
139
# File 'lib/couch_potato/database.rb', line 132

def load!(id)
  doc = load(id)
  if id.is_a?(Array)
    missing_docs = id - doc.map(&:id)
  end
  raise(CouchPotato::NotFound, missing_docs.try(:join, ', ')) if doc.nil? || missing_docs.try(:any?)
  doc
end

#load_document(id) ⇒ Object Also known as: load

loads a document by its id(s) id - either a single id or an array of ids returns either a single document or an array of documents (if an array of ids was passed). returns nil if the single document could not be found. when passing an array and some documents could not be found these are omitted from the returned array



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/couch_potato/database.rb', line 113

def load_document(id)
  cached = cache && id.is_a?(String) && cache[id]
  if cache
    if cached
      ActiveSupport::Notifications.instrument('couch_potato.load.cached') do
        cached
      end
    else
      cache[id] = load_document_without_caching(id)
      cache[id]
    end
  else
    load_document_without_caching(id)
  end
end

#save_document(document, validate = true, retries = 0, &block) ⇒ Object Also known as: save

saves a document. returns true on success, false on failure. if passed a block will:

  • yield the object to be saved to the block and run if once before saving

  • on conflict: reload the document, run the block again and retry saving



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/couch_potato/database.rb', line 77

def save_document(document, validate = true, retries = 0, &block)
  cache&.clear
  begin
    block.call document if block
    save_document_without_conflict_handling(document, validate)
  rescue CouchRest::Conflict
    if block
      handle_write_conflict document, validate, retries, &block
    else
      raise CouchPotato::Conflict.new
    end
  end
end

#save_document!(document) ⇒ Object Also known as: save!

saves a document, raises a CouchPotato::Database::ValidationsFailedError on failure



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

def save_document!(document)
  save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
end

#switch_to(database_name) ⇒ Object

returns a new database instance connected to the CouchDB database with the given name. the name is passed through the additional_databases configuration to resolve it to a database configured there. if the current database has a cache, the new database will receive a cleared copy of it.



156
157
158
159
160
161
162
# File 'lib/couch_potato/database.rb', line 156

def switch_to(database_name)
  resolved_database_name = CouchPotato.resolve_database_name(database_name)
  self
    .class
    .new(CouchPotato.couchrest_database_for_name(resolved_database_name), name: database_name)
    .tap(&copy_clear_cache_proc)
end

#switch_to_defaultObject

returns a new database instance connected to the default CouchDB database. if the current database has a cache, the new database will receive a cleared copy of it.



167
168
169
170
171
172
# File 'lib/couch_potato/database.rb', line 167

def switch_to_default
  self
    .class
    .new(CouchPotato.couchrest_database)
    .tap(&copy_clear_cache_proc)
end

#view(spec) ⇒ Object

executes a view and return the results. you pass in a view spec which is usually a result of a SomePersistentClass.some_view call.

Example:

class User
  include CouchPotato::Persistence
  property :age
  view :all, key: :age
end
db = CouchPotato.database

db.view(User.all) # => [user1, user2]

You can pass the usual parameters you can pass to a couchdb view to the view:

db.view(User.all(limit: 5, startkey: 2, reduce: false))

For your convenience when passing a hash with only a key parameter you can just pass in the value

db.view(User.all(key: 1)) == db.view(User.all(1))

Instead of passing a startkey and endkey you can pass in a key with a range:

db.view(User.all(key: 1..20)) == db.view(startkey: 1, endkey: 20) == db.view(User.all(1..20))

You can also pass in multiple keys:

db.view(User.all(keys: [1, 2, 3]))


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/couch_potato/database.rb', line 45

def view(spec)
  id = view_cache_id(spec)
  cached = cache && id.is_a?(String) && cache[id]
  if cache
    if cached
      ActiveSupport::Notifications.instrument('couch_potato.view.cached') do
        cached
      end
    else
      cache[id] = view_without_caching(spec)
      cache[id]
    end
  else
    view_without_caching(spec)
  end
end