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
- #_find_by_key_as_object_id(collection, id) ⇒ Object
- #_find_by_key_as_provided(collection, id) ⇒ Object
- #_normalize_query(query) ⇒ Object
- #client ⇒ Object
- #default_db_name ⇒ Object
- #delete(collection_name, id) ⇒ Object
- #find_all(collection_name) ⇒ 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
93 94 95 |
# File 'lib/curator/mongo/data_store.rb', line 93 def _collection(name) _db.collection(name) end |
#_db ⇒ Object
97 98 99 |
# File 'lib/curator/mongo/data_store.rb', line 97 def _db client.db(_db_name) end |
#_db_name ⇒ Object
105 106 107 |
# File 'lib/curator/mongo/data_store.rb', line 105 def _db_name @database_name end |
#_find_by_key_as_object_id(collection, id) ⇒ Object
87 88 89 90 91 |
# File 'lib/curator/mongo/data_store.rb', line 87 def _find_by_key_as_object_id(collection, id) if BSON::ObjectId.legal?(id) collection.find_one(:_id => BSON::ObjectId.from_string(id)) end end |
#_find_by_key_as_provided(collection, id) ⇒ Object
83 84 85 |
# File 'lib/curator/mongo/data_store.rb', line 83 def _find_by_key_as_provided(collection, id) collection.find_one(:_id => id) end |
#_normalize_query(query) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/curator/mongo/data_store.rb', line 114 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/curator/mongo/data_store.rb', line 7 def client return @client if @client if Curator.config.client @client = Curator.config.client @database_name = Curator.config.database return @client end if ENV['MONGO_URI'] require 'uri' uri = URI.parse(ENV['MONGO_URI']) @database_name = uri.path.gsub(/^\//, '') @client = ::Mongo::Connection.from_uri(uri.to_s) return @client end config = YAML.load(File.read(Curator.config.mongo_config_file))[Curator.config.environment] config = config.symbolize_keys host = config.delete(:host) port = config.delete(:port) password = ENV['MONGO_PASSWORD'] || config.delete(:password) username = ENV['MONGO_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
101 102 103 |
# File 'lib/curator/mongo/data_store.rb', line 101 def default_db_name "#{Curator.config.database}:#{Curator.config.environment}" end |
#delete(collection_name, id) ⇒ Object
56 57 58 59 |
# File 'lib/curator/mongo/data_store.rb', line 56 def delete(collection_name, id) collection = _collection(collection_name) collection.remove(:_id => id) end |
#find_all(collection_name) ⇒ Object
61 62 63 64 65 |
# File 'lib/curator/mongo/data_store.rb', line 61 def find_all(collection_name) collection = _collection(collection_name) documents = collection.find documents.map {|doc| normalize_document(doc) } end |
#find_by_attribute(collection_name, field, query) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/curator/mongo/data_store.rb', line 67 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
77 78 79 80 81 |
# File 'lib/curator/mongo/data_store.rb', line 77 def find_by_key(collection_name, id) collection = _collection(collection_name) document = _find_by_key_as_provided(collection, id) || _find_by_key_as_object_id(collection, id) normalize_document(document) unless document.nil? end |
#normalize_document(doc) ⇒ Object
109 110 111 112 |
# File 'lib/curator/mongo/data_store.rb', line 109 def normalize_document(doc) key = doc.delete '_id' Hash[:key => key, :data => doc] end |
#remove_all_keys ⇒ Object
37 38 39 |
# File 'lib/curator/mongo/data_store.rb', line 37 def remove_all_keys self.reset! end |
#reset! ⇒ Object
41 42 43 |
# File 'lib/curator/mongo/data_store.rb', line 41 def reset! _db.collections.each {|coll| coll.drop unless coll.name =~ /system/ } end |
#save(options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/curator/mongo/data_store.rb', line 45 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 |