Class: RelaxDB::AllDelegator
- Inherits:
-
Delegator
- Object
- Delegator
- RelaxDB::AllDelegator
- 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.sorted_by(:att1, :att2) - returns all docs in CouchDB of type FooDoc sorted by att1, then att2
FooDoc.all.sorted_by(:att1) { |q| q.key("bar") } - returns all docs of type FooDoc where att1 equals "bar"
FooDoc.all.destroy! - does what it says on the tin
FooDoc.all.size - issues a query to a reduce function that returns the total number of docs for that class
Instance Method Summary collapse
- #__getobj__ ⇒ Object
-
#destroy! ⇒ Object
Note that this method leaves the corresponding DesignDoc for the associated class intact.
-
#initialize(class_name) ⇒ AllDelegator
constructor
A new instance of AllDelegator.
-
#size ⇒ Object
This is pretty ugly - this pattern is now spread over three places (sorted_by_view, relaxdb and here) Consolidation needed.
- #sorted_by(*atts) {|query| ... } ⇒ Object
Constructor Details
#initialize(class_name) ⇒ AllDelegator
Returns a new instance of AllDelegator.
13 14 15 16 |
# File 'lib/relaxdb/all_delegator.rb', line 13 def initialize(class_name) super([]) @class_name = class_name end |
Instance Method Details
#__getobj__ ⇒ Object
18 19 20 21 22 23 |
# File 'lib/relaxdb/all_delegator.rb', line 18 def __getobj__ view_path = "_design/#{@class_name}/_view/all?reduce=false" map, reduce = ViewCreator.all(@class_name) RelaxDB.retrieve(view_path, @class_name, "all", map, reduce) end |
#destroy! ⇒ Object
Note that this method leaves the corresponding DesignDoc for the associated class intact
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/relaxdb/all_delegator.rb', line 35 def destroy! each do |o| # A reload is required for deleting objects with a self referential references_many relationship # This makes all.destroy! very slow. Given that references_many is now deprecated and will # soon be removed, the required reload is no longer performed. # obj = RelaxDB.load(o._id) # obj.destroy! o.destroy! end end |
#size ⇒ Object
This is pretty ugly - this pattern is now spread over three places (sorted_by_view, relaxdb and here) Consolidation needed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/relaxdb/all_delegator.rb', line 50 def size view_path = "_design/#{@class_name}/_view/all" map, reduce = ViewCreator.all(@class_name) begin resp = RelaxDB.db.get(view_path) rescue => e DesignDocument.get(@class_name).add_map_view("all", map). add_reduce_view("all", reduce).save resp = RelaxDB.db.get(view_path) end data = JSON.parse(resp.body) data["rows"][0] ? data["rows"][0]["value"] : 0 end |
#sorted_by(*atts) {|query| ... } ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/relaxdb/all_delegator.rb', line 25 def sorted_by(*atts) view = SortedByView.new(@class_name, *atts) query = Query.new(@class_name, view.view_name) yield query if block_given? view.query(query) end |