Class: Rack::JSON::Collection
- Inherits:
-
Object
- Object
- Rack::JSON::Collection
- Defined in:
- lib/rackjson/collection.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #create(document) ⇒ Object
- #delete(selector = {}) ⇒ Object
- #delete_all ⇒ Object
- #exists?(selector) ⇒ Boolean
- #find(selector, options = {}) ⇒ Object
-
#initialize(collection) ⇒ Collection
constructor
A new instance of Collection.
- #save(document) ⇒ Object
- #update(selector, document, query = {}) ⇒ Object
Constructor Details
#initialize(collection) ⇒ Collection
Returns a new instance of Collection.
3 4 5 |
# File 'lib/rackjson/collection.rb', line 3 def initialize(collection) @collection = collection end |
Instance Method Details
#all(options = {}) ⇒ Object
7 8 9 |
# File 'lib/rackjson/collection.rb', line 7 def all(={}) @collection.find({}, ).to_a end |
#create(document) ⇒ Object
11 12 13 |
# File 'lib/rackjson/collection.rb', line 11 def create(document) @collection.save(document) end |
#delete(selector = {}) ⇒ Object
15 16 17 |
# File 'lib/rackjson/collection.rb', line 15 def delete(selector={}) @collection.remove(prepared(selector)) end |
#delete_all ⇒ Object
19 20 21 |
# File 'lib/rackjson/collection.rb', line 19 def delete_all @collection.remove end |
#exists?(selector) ⇒ Boolean
23 24 25 |
# File 'lib/rackjson/collection.rb', line 23 def exists?(selector) !@collection.find(prepared(selector)).to_a.empty? end |
#find(selector, options = {}) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/rackjson/collection.rb', line 27 def find(selector, ={}) if selector.is_a? Hash @collection.find(selector, ).inject([]) {|documents, row| documents << Rack::JSON::Document.new(row)} else Rack::JSON::Document.new(@collection.find_one(:_id => selector)) end end |
#save(document) ⇒ Object
35 36 37 |
# File 'lib/rackjson/collection.rb', line 35 def save(document) @collection.save(document) end |
#update(selector, document, query = {}) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/rackjson/collection.rb', line 39 def update(selector, document, query={}) if exists?(prepared(selector).merge(query)) @collection.update(prepared(selector).merge(query), document, :upsert => false) else false end end |