Class: CouchPotato::Database

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

Defined Under Namespace

Classes: ValidationsFailedError

Instance Method Summary collapse

Constructor Details

#initialize(couchrest_database) ⇒ Database

Returns a new instance of Database.



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

def initialize(couchrest_database)
  @couchrest_database = couchrest_database
end

Instance Method Details

#couchrest_databaseObject

returns the underlying CouchRest::Database instance



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

def couchrest_database
  @couchrest_database
end

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



96
97
98
99
100
101
102
# File 'lib/couch_potato/database.rb', line 96

def destroy_document(document)
  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



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

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



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

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

#inspectObject

:nodoc:



136
137
138
# File 'lib/couch_potato/database.rb', line 136

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



127
128
129
130
131
132
133
134
# File 'lib/couch_potato/database.rb', line 127

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



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/couch_potato/database.rb', line 110

def load_document(id)
  raise "Can't load a document without an id (got nil)" if id.nil?

  ActiveSupport::Notifications.instrument('couch_potato.load') do
    if id.is_a?(Array)
      bulk_load id
    else
      instance = couchrest_database.get(id)
      instance.database = self if instance
      instance
    end
  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



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

def save_document(document, validate = true, retries = 0, &block)
  begin
    block.call document if block
    save_document_without_conflict_handling(document, validate)
  rescue CouchRest::Conflict => e
    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



91
92
93
# File 'lib/couch_potato/database.rb', line 91

def save_document!(document)
  save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
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]))


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/couch_potato/database.rb', line 39

def view(spec)
  ActiveSupport::Notifications.instrument('couch_potato.view', name: "#{spec.design_document}/#{spec.view_name}") do
    results = CouchPotato::View::ViewQuery.new(
      couchrest_database,
      spec.design_document,
      {spec.view_name => {
        :map => spec.map_function,
        :reduce => spec.reduce_function
      }
      },
      ({spec.list_name => spec.list_function} unless spec.list_name.nil?),
      spec.lib,
      spec.language
    ).query_view!(spec.view_parameters)
    processed_results = spec.process_results results
    processed_results.each do |document|
      document.database = self if document.respond_to?(:database=)
    end if processed_results.respond_to?(:each)
    processed_results
  end
end