Class: Curator::Mongo::DataStore
- Inherits:
-
Object
- Object
- Curator::Mongo::DataStore
- Defined in:
- lib/curator/mongo/data_store.rb
Class Method Summary collapse
- ._collection(name) ⇒ Object
- ._collection_name(name) ⇒ Object
- ._db ⇒ Object
- ._db_name ⇒ Object
- .client ⇒ Object
- .delete(collection_name, id) ⇒ Object
- .find_by_index(collection_name, field, query) ⇒ Object
- .find_by_key(collection_name, id) ⇒ Object
- .normalize_document(doc) ⇒ Object
- .remove_all_keys ⇒ Object
- .reset! ⇒ Object
- .save(options) ⇒ Object
Class Method Details
._collection(name) ⇒ Object
54 55 56 |
# File 'lib/curator/mongo/data_store.rb', line 54 def self._collection(name) _db.collection(name) end |
._collection_name(name) ⇒ Object
58 59 60 |
# File 'lib/curator/mongo/data_store.rb', line 58 def self._collection_name(name) _db.collection(name).name end |
._db ⇒ Object
62 63 64 |
# File 'lib/curator/mongo/data_store.rb', line 62 def self._db client.db(_db_name) end |
._db_name ⇒ Object
66 67 68 |
# File 'lib/curator/mongo/data_store.rb', line 66 def self._db_name "#{Curator.config.database}:#{Curator.config.environment}" end |
.client ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/curator/mongo/data_store.rb', line 7 def self.client return @client if @client config = YAML.load(File.read(Curator.config.mongo_config_file))[Curator.config.environment] host = config.delete(:host) port = config.delete(:port) @client = ::Mongo::Connection.new(host, port, config) end |
.delete(collection_name, id) ⇒ Object
35 36 37 38 |
# File 'lib/curator/mongo/data_store.rb', line 35 def self.delete(collection_name, id) collection = _collection(collection_name) collection.remove(:_id => id) end |
.find_by_index(collection_name, field, query) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/curator/mongo/data_store.rb', line 40 def self.find_by_index(collection_name, field, query) return [] if query.nil? collection = _collection(collection_name) documents = collection.find(field.to_sym => query) documents.map {|doc| normalize_document(doc) } end |
.find_by_key(collection_name, id) ⇒ Object
48 49 50 51 52 |
# File 'lib/curator/mongo/data_store.rb', line 48 def self.find_by_key(collection_name, id) collection = _collection(collection_name) document = collection.find_one({:_id => id}) normalize_document(document) unless document.nil? end |
.normalize_document(doc) ⇒ Object
70 71 72 73 |
# File 'lib/curator/mongo/data_store.rb', line 70 def self.normalize_document(doc) key = doc.delete '_id' Hash[:key => key, :data => doc] end |
.remove_all_keys ⇒ Object
15 16 17 |
# File 'lib/curator/mongo/data_store.rb', line 15 def self.remove_all_keys self.reset! end |
.reset! ⇒ Object
19 20 21 |
# File 'lib/curator/mongo/data_store.rb', line 19 def self.reset! _db.collections.each {|coll| coll.drop unless coll.name =~ /system/ } end |
.save(options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/curator/mongo/data_store.rb', line 23 def self.save() collection = _collection [:collection_name] key = .delete(:key) document = [:value] document.merge!({:_id => key}) unless key.nil? .fetch(:index, {}).each do |index_name, index_value| collection.ensure_index index_name end object_id = collection.save document Hash[:key => object_id] end |