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
# File 'lib/relaxdb/all_delegator.rb', line 17

def __getobj__
  unless @objs
    @objs = RelaxDB.rf_view "#{@class_name}_all", @params
  end
  @objs
end

#destroy!Object

TODO: destroy in a bulk_save if feasible



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/relaxdb/all_delegator.rb', line 30

def destroy!
  __getobj__
  @objs.each do |o| 
    # A reload is required for deleting objects with a self referential references_many relationship
    # This makes all.destroy! very slow. Change if needed
    # obj = RelaxDB.load(o._id)
    # obj.destroy!
    
    o.destroy!
  end
end

#sizeObject



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

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