Class: Pupa::Processor::Connection::MongoDBAdapter
- Inherits:
-
Object
- Object
- Pupa::Processor::Connection::MongoDBAdapter
- Defined in:
- lib/pupa/processor/connection_adapters/mongodb_adapter.rb
Overview
A proxy class to save plain old Ruby objects to MongoDB.
Instance Attribute Summary collapse
-
#raw_connection ⇒ Object
readonly
Returns the value of attribute raw_connection.
Instance Method Summary collapse
-
#find(selector) ⇒ Hash?
Finds a document matching the selection criteria.
-
#initialize(database_url) ⇒ MongoDBAdapter
constructor
A new instance of MongoDBAdapter.
-
#save(object) ⇒ Array
Inserts or replaces a document in MongoDB.
Constructor Details
#initialize(database_url) ⇒ MongoDBAdapter
Returns a new instance of MongoDBAdapter.
11 12 13 14 15 |
# File 'lib/pupa/processor/connection_adapters/mongodb_adapter.rb', line 11 def initialize(database_url) uri = URI.parse(database_url) @raw_connection = Mongo::Client.new(["#{uri.host}:#{uri.port}"], database: uri.path[1..-1]) @raw_connection.login(uri.user, uri.password) if uri.user && uri.password end |
Instance Attribute Details
#raw_connection ⇒ Object (readonly)
Returns the value of attribute raw_connection.
8 9 10 |
# File 'lib/pupa/processor/connection_adapters/mongodb_adapter.rb', line 8 def raw_connection @raw_connection end |
Instance Method Details
#find(selector) ⇒ Hash?
Finds a document matching the selection criteria.
The selection criteria must set a _type
key in order to determine
the collection to query.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pupa/processor/connection_adapters/mongodb_adapter.rb', line 25 def find(selector) collection_name = collection_name_from_class_name(selector[:_type].camelize) if selector.except(:_type).empty? raise Errors::EmptySelectorError, "selector is empty during find in collection #{collection_name}" end collection = raw_connection[collection_name] query = collection.find(selector) case query.count when 0 nil when 1 query.first else raise Errors::TooManyMatches, "selector matches multiple documents during find in collection #{collection_name}: #{JSON.dump(selector)}" end end |
#save(object) ⇒ Array
Inserts or replaces a document in MongoDB.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pupa/processor/connection_adapters/mongodb_adapter.rb', line 48 def save(object) selector = object.fingerprint collection_name = collection_name_from_class_name(object.class.to_s) if selector.empty? raise Errors::EmptySelectorError, "selector is empty during save in collection #{collection_name} for #{object._id}" end collection = raw_connection[collection_name] query = collection.find(selector) # Run query before callbacks to avoid e.g. timestamps in the selector. case query.count when 0 object.run_callbacks(:save) do object.run_callbacks(:create) do collection.insert_one(object.to_h(persist: true)) [true, object._id.to_s] end end when 1 # Make the document available to the callbacks. # @see https://github.com/jpmckinney/pupa-ruby/issues/17 object.document = query.first object.run_callbacks(:save) do query.update_one(object.to_h(persist: true).except(:_id)) [false, object.document['_id'].to_s] end else raise Errors::TooManyMatches, "selector matches multiple documents during save in collection #{collection_name} for #{object._id}: #{JSON.dump(selector)}" end end |