Class: RelaxDB::AllDelegator

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

Overview

The AllDelegator allows clients to query CouchDB in a natural way

FooDoc.all - returns all docs in CouchDB of type FooDoc
FooDoc.all.size - issues a query to a reduce function that returns the total number of docs for that class
FooDoc.all.destroy! - TODO - better description

Instance Method Summary collapse

Constructor Details

#initialize(class_name, params) ⇒ AllDelegator

Returns a new instance of AllDelegator.



11
12
13
14
15
# File 'lib/relaxdb/all_delegator.rb', line 11

def initialize(class_name, params)
  super([])
  @class_name = class_name
  @params = params
end

Instance Method Details

#__getobj__Object



17
18
19
20
21
22
23
24
# File 'lib/relaxdb/all_delegator.rb', line 17

def __getobj__
  unless @ids
    params = {:raw => true}.merge @params
    result = RelaxDB.rf_view "#{@class_name}_all", params
    @ids = RelaxDB.ids_from_view result
  end
  @ids
end

#__setobj__(obj) ⇒ Object



26
27
28
# File 'lib/relaxdb/all_delegator.rb', line 26

def __setobj__ obj
  # Intentionally empty
end

#destroy!Object



40
41
42
43
44
45
46
# File 'lib/relaxdb/all_delegator.rb', line 40

def destroy!
  load!
  @objs.each { |o| o.data["_deleted"] = true }
  # Direct post rather than bulk save as we don't want validators to be run
  resp = RelaxDB.db.post("_bulk_docs", {"docs" => @objs}.to_json)
  JSON.parse resp.body
end

#load!Object



30
31
32
33
# File 'lib/relaxdb/all_delegator.rb', line 30

def load!
  __getobj__
  @objs = RelaxDB.load! @ids
end

#sizeObject



35
36
37
38
# File 'lib/relaxdb/all_delegator.rb', line 35

def size
  size = RelaxDB.view "#{@class_name}_all", :reduce => true
  size || 0
end