Class: CouchRest::Database

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

Instance Method Summary collapse

Instance Method Details

#casted_get(id, params = {}) ⇒ Object

Just like get, but casts the returned document as a CouchRest::Model, if there is a matching class available

Currently, casted_get doesn’t support given blocks.



34
35
36
37
38
39
40
41
# File 'lib/couchrest/database/casted_queries.rb', line 34

def casted_get(id, params = {})
  doc = get(id, params)
  return nil unless doc
  return doc unless type = doc['couchrest-type'] || doc['type']
  klass = type.constantize
  return doc unless klass.respond_to? :build_from_database
  klass.build_from_database(doc)
end

#casted_view(name, params = {}) ⇒ Object

Just like view, but automatically includes the documents in the response and casts each document as a CouchRest::Model class, if possible.

Currently, casted_view doesn’t support given blocks.



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

def casted_view(name, params = {})
  # force the DB response to include documents
  params[:include_docs] = true
  response = view(name, params)
  rows = response['rows']
  return response unless rows

  response['rows'] = rows.map do |row|
    doc = row['doc']
    type = doc['couchrest-type'] || doc['type']
    if type
      klass = type.constantize
      if klass.respond_to? :build_from_database
        row['doc'] = klass.build_from_database(doc)
      end
    end
    row
  end

  response
end