Class: Rack::JSON::Collection
- Inherits:
-
Object
- Object
- Rack::JSON::Collection
- Defined in:
- lib/rackjson/collection.rb
Instance Method Summary collapse
- #decrement(selector, field, value = 1) ⇒ Object
- #delete(selector = {}) ⇒ Object
- #delete_field(selector, field) ⇒ Object
- #exists?(selector) ⇒ Boolean
- #find(selector, options = {}) ⇒ Object
- #find_field(selector, fields, options = {}) ⇒ Object
- #find_one(selector, options = {}) ⇒ Object
- #increment(selector, field, value = 1) ⇒ Object
-
#initialize(collection) ⇒ Collection
constructor
A new instance of Collection.
- #save(document) ⇒ Object
- #update(selector, document, query = {}) ⇒ Object
- #update_field(selector, field, value) ⇒ Object
Constructor Details
#initialize(collection) ⇒ Collection
Returns a new instance of Collection.
6 7 8 |
# File 'lib/rackjson/collection.rb', line 6 def initialize(collection) @collection = collection end |
Instance Method Details
#decrement(selector, field, value = 1) ⇒ Object
35 36 37 |
# File 'lib/rackjson/collection.rb', line 35 def decrement(selector, field, value=1) _update(prepared(selector), { "$inc" => { dot_notate(field) => -1 * (value || 1) }, "$set" => { :updated_at => Time.now }}) end |
#delete(selector = {}) ⇒ Object
10 11 12 |
# File 'lib/rackjson/collection.rb', line 10 def delete(selector={}) @collection.remove(prepared(selector)) end |
#delete_field(selector, field) ⇒ Object
14 15 16 |
# File 'lib/rackjson/collection.rb', line 14 def delete_field(selector, field) _update(prepared(selector), { "$unset" => { dot_notate(field) => 1 }, "$set" => { :updated_at => Time.now }}) end |
#exists?(selector) ⇒ Boolean
18 19 20 |
# File 'lib/rackjson/collection.rb', line 18 def exists?(selector) !@collection.find(prepared(selector)).to_a.empty? end |
#find(selector, options = {}) ⇒ Object
22 23 24 |
# File 'lib/rackjson/collection.rb', line 22 def find(selector, ={}) @collection.find(selector, ).inject([]) {|documents, row| documents << Rack::JSON::Document.create(row)} end |
#find_field(selector, fields, options = {}) ⇒ Object
26 27 28 29 |
# File 'lib/rackjson/collection.rb', line 26 def find_field(selector, fields, ={}) document = find_one(prepared(selector)) document ? document.field(fields) : nil end |
#find_one(selector, options = {}) ⇒ Object
31 32 33 |
# File 'lib/rackjson/collection.rb', line 31 def find_one(selector, ={}) find(prepared(selector), .merge(:limit => 0)).first end |
#increment(selector, field, value = 1) ⇒ Object
39 40 41 |
# File 'lib/rackjson/collection.rb', line 39 def increment(selector, field, value=1) _update(prepared(selector), { "$inc" => { dot_notate(field) => value || 1 }, "$set" => { :updated_at => Time.now }}) end |
#save(document) ⇒ Object
50 51 52 |
# File 'lib/rackjson/collection.rb', line 50 def save(document) @collection.save(document.to_h) end |
#update(selector, document, query = {}) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/rackjson/collection.rb', line 54 def update(selector, document, query={}) if exists?(prepared(selector).merge(query)) _update(prepared(selector).merge(query), document.to_h, :upsert => false) else false end end |
#update_field(selector, field, value) ⇒ Object
62 63 64 |
# File 'lib/rackjson/collection.rb', line 62 def update_field(selector, field, value) _update(prepared(selector), { "$set" => { dot_notate(field) => value, :updated_at => Time.now }}) end |