Class: Curator::Mongo::DataStore
- Inherits:
-
Object
- Object
- Curator::Mongo::DataStore
- Defined in:
- lib/curator/mongo/data_store.rb
Instance Method Summary collapse
- #_collection(name) ⇒ Object
- #_db ⇒ Object
- #_db_name ⇒ Object
- #_normalize_query(query) ⇒ Object
- #client ⇒ Object
- #default_db_name ⇒ Object
- #delete(collection_name, id) ⇒ Object
- #find_by_attribute(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
Instance Method Details
#_collection(name) ⇒ Object
60 61 62 |
# File 'lib/curator/mongo/data_store.rb', line 60 def _collection(name) _db.collection(name) end |
#_db ⇒ Object
64 65 66 |
# File 'lib/curator/mongo/data_store.rb', line 64 def _db client.db(_db_name) end |
#_db_name ⇒ Object
72 73 74 |
# File 'lib/curator/mongo/data_store.rb', line 72 def _db_name @database_name end |
#_normalize_query(query) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/curator/mongo/data_store.rb', line 81 def _normalize_query(query) query.inject({}) do |hash, (key, value)| case value when Range hash[key] = {'$gte' => value.first, '$lt' => value.last} else hash[key] = value end hash end end |
#client ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/curator/mongo/data_store.rb', line 7 def 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) password = config.delete(:password) username = config.delete(:username) @database_name = config.delete(:database) || default_db_name @client = ::Mongo::Connection.new(host, port, config) @client.add_auth(@database_name, username, password) if username and password @client end |
#default_db_name ⇒ Object
68 69 70 |
# File 'lib/curator/mongo/data_store.rb', line 68 def default_db_name "#{Curator.config.database}:#{Curator.config.environment}" end |
#delete(collection_name, id) ⇒ Object
39 40 41 42 |
# File 'lib/curator/mongo/data_store.rb', line 39 def delete(collection_name, id) collection = _collection(collection_name) collection.remove(:_id => id) end |
#find_by_attribute(collection_name, field, query) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/curator/mongo/data_store.rb', line 44 def find_by_attribute(collection_name, field, query) return [] if query.nil? exp = {} exp[field] = query collection = _collection(collection_name) documents = collection.find(_normalize_query(exp)) documents.map {|doc| normalize_document(doc) } end |
#find_by_key(collection_name, id) ⇒ Object
54 55 56 57 58 |
# File 'lib/curator/mongo/data_store.rb', line 54 def 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
76 77 78 79 |
# File 'lib/curator/mongo/data_store.rb', line 76 def normalize_document(doc) key = doc.delete '_id' Hash[:key => key, :data => doc] end |
#remove_all_keys ⇒ Object
20 21 22 |
# File 'lib/curator/mongo/data_store.rb', line 20 def remove_all_keys self.reset! end |
#reset! ⇒ Object
24 25 26 |
# File 'lib/curator/mongo/data_store.rb', line 24 def reset! _db.collections.each {|coll| coll.drop unless coll.name =~ /system/ } end |
#save(options) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/curator/mongo/data_store.rb', line 28 def 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 collection.save document end |