Module: MinceMongoDb::Interface
- Defined in:
- lib/mince_mongo_db/interface.rb
Class Method Summary collapse
- .add(collection_name, hash) ⇒ Object
- .all_before(collection_name, key, value) ⇒ Object
- .array_contains(collection_name, key, value) ⇒ Object
- .clear ⇒ Object
- .collection(collection_name) ⇒ Object
- .containing_any(collection_name, key, values) ⇒ Object
- .data ⇒ Object
- .db ⇒ Object
- .delete_by_params(collection_name, params) ⇒ Object
- .delete_collection(collection_name) ⇒ Object
- .delete_field(collection_name, key) ⇒ Object
- .find(collection_name, value) ⇒ Object
- .find_all(collection_name) ⇒ Object
- .generate_unique_id(*args) ⇒ Object
- .get_all_for_key_with_value(collection_name, key, value) ⇒ Object
- .get_by_params(collection_name, hash) ⇒ Object
- .get_for_key_with_value(collection_name, key, value) ⇒ Object
- .increment_field_by_amount(collection_name, primary_key_value, field_name, amount) ⇒ Object
- .insert(collection_name, data) ⇒ Object
- .primary_key ⇒ Object
- .push_to_array(collection_name, identifying_value, array_key, value_to_push) ⇒ Object
- .remove_from_array(collection_name, identifying_value, array_key, value_to_remove) ⇒ Object
- .replace(collection_name, hash) ⇒ Object
- .set_data(data) ⇒ Object
- .update_field_with_value(collection_name, primary_key_value, field_name, new_value) ⇒ Object
Class Method Details
.add(collection_name, hash) ⇒ Object
22 23 24 |
# File 'lib/mince_mongo_db/interface.rb', line 22 def self.add(collection_name, hash) collection(collection_name).insert(hash) end |
.all_before(collection_name, key, value) ⇒ Object
66 67 68 |
# File 'lib/mince_mongo_db/interface.rb', line 66 def self.all_before(collection_name, key, value) collection(collection_name).find({key.to_s => {"$lt" => value}}) end |
.array_contains(collection_name, key, value) ⇒ Object
74 75 76 |
# File 'lib/mince_mongo_db/interface.rb', line 74 def self.array_contains(collection_name, key, value) collection(collection_name).find(key.to_s => value) end |
.clear ⇒ Object
78 79 80 81 82 |
# File 'lib/mince_mongo_db/interface.rb', line 78 def self.clear db.collection_names.each do |collection_name| db.drop_collection collection_name unless collection_name.include?('system') end end |
.collection(collection_name) ⇒ Object
108 109 110 |
# File 'lib/mince_mongo_db/interface.rb', line 108 def self.collection(collection_name) DataStore.collection(collection_name) end |
.containing_any(collection_name, key, values) ⇒ Object
70 71 72 |
# File 'lib/mince_mongo_db/interface.rb', line 70 def self.containing_any(collection_name, key, values) collection(collection_name).find({key.to_s => {"$in" => values}}) end |
.data ⇒ Object
102 103 104 105 106 |
# File 'lib/mince_mongo_db/interface.rb', line 102 def self.data db.collection_names.map do |collection_name| find_all collection_name end end |
.db ⇒ Object
112 113 114 |
# File 'lib/mince_mongo_db/interface.rb', line 112 def self.db DataStore.db end |
.delete_by_params(collection_name, params) ⇒ Object
18 19 20 |
# File 'lib/mince_mongo_db/interface.rb', line 18 def self.delete_by_params(collection_name, params) collection(collection_name).remove(params) end |
.delete_collection(collection_name) ⇒ Object
84 85 86 |
# File 'lib/mince_mongo_db/interface.rb', line 84 def self.delete_collection(collection_name) collection(collection_name).drop end |
.delete_field(collection_name, key) ⇒ Object
14 15 16 |
# File 'lib/mince_mongo_db/interface.rb', line 14 def self.delete_field(collection_name, key) collection(collection_name).update({}, {"$unset" => { key => 1 } }, multi: true) end |
.find(collection_name, value) ⇒ Object
54 55 56 |
# File 'lib/mince_mongo_db/interface.rb', line 54 def self.find(collection_name, value) collection(collection_name).find_one({primary_key.to_s => value}) end |
.find_all(collection_name) ⇒ Object
50 51 52 |
# File 'lib/mince_mongo_db/interface.rb', line 50 def self.find_all(collection_name) collection(collection_name).find end |
.generate_unique_id(*args) ⇒ Object
6 7 8 |
# File 'lib/mince_mongo_db/interface.rb', line 6 def self.generate_unique_id(*args) BSON::ObjectId.new.to_s end |
.get_all_for_key_with_value(collection_name, key, value) ⇒ Object
38 39 40 |
# File 'lib/mince_mongo_db/interface.rb', line 38 def self.get_all_for_key_with_value(collection_name, key, value) get_by_params(collection_name, key.to_s => value) end |
.get_by_params(collection_name, hash) ⇒ Object
46 47 48 |
# File 'lib/mince_mongo_db/interface.rb', line 46 def self.get_by_params(collection_name, hash) collection(collection_name).find(hash) end |
.get_for_key_with_value(collection_name, key, value) ⇒ Object
42 43 44 |
# File 'lib/mince_mongo_db/interface.rb', line 42 def self.get_for_key_with_value(collection_name, key, value) get_all_for_key_with_value(collection_name, key, value).first end |
.increment_field_by_amount(collection_name, primary_key_value, field_name, amount) ⇒ Object
34 35 36 |
# File 'lib/mince_mongo_db/interface.rb', line 34 def self.increment_field_by_amount(collection_name, primary_key_value, field_name, amount) collection(collection_name).update({primary_key => primary_key_value}, {'$inc' => { field_name => amount } }) end |
.insert(collection_name, data) ⇒ Object
88 89 90 91 92 |
# File 'lib/mince_mongo_db/interface.rb', line 88 def self.insert(collection_name, data) data.each do |datum| add collection_name, datum end end |
.primary_key ⇒ Object
10 11 12 |
# File 'lib/mince_mongo_db/interface.rb', line 10 def self.primary_key Config.primary_key end |
.push_to_array(collection_name, identifying_value, array_key, value_to_push) ⇒ Object
58 59 60 |
# File 'lib/mince_mongo_db/interface.rb', line 58 def self.push_to_array(collection_name, , array_key, value_to_push) collection(collection_name).update({primary_key.to_s => }, {'$push' => {array_key.to_s => value_to_push}}) end |
.remove_from_array(collection_name, identifying_value, array_key, value_to_remove) ⇒ Object
62 63 64 |
# File 'lib/mince_mongo_db/interface.rb', line 62 def self.remove_from_array(collection_name, , array_key, value_to_remove) collection(collection_name).update({primary_key.to_s => }, {'$pull' => {array_key.to_s => value_to_remove}}) end |
.replace(collection_name, hash) ⇒ Object
26 27 28 |
# File 'lib/mince_mongo_db/interface.rb', line 26 def self.replace(collection_name, hash) collection(collection_name).update({primary_key => hash[primary_key]}, hash) end |
.set_data(data) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/mince_mongo_db/interface.rb', line 94 def self.set_data(data) clear data.each do |key, value| insert key, value end end |
.update_field_with_value(collection_name, primary_key_value, field_name, new_value) ⇒ Object
30 31 32 |
# File 'lib/mince_mongo_db/interface.rb', line 30 def self.update_field_with_value(collection_name, primary_key_value, field_name, new_value) collection(collection_name).update({primary_key => primary_key_value}, {'$set' => { field_name => new_value } }) end |