diff --git a/Gemfile.lock b/Gemfile.lock index ecd9686..ecce356 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - mongo_api (0.1.0) + mongo_api (0.1.1) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index 0246057..02195c2 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ user = Users.destroy(where: { name: 'Jane Smith' }) # => {"deletedCount"=>1} # Destroy All users # @param [Hash] where: The query predicate. -user = Userss.destroy_all(where: { name: 'Jane Smith' }) # => {"deletedCount"=>1} +user = Users.destroy_all(where: { name: 'Jane Smith' }) # => {"deletedCount"=>1} # Count users user = Users.count # => 10 @@ -87,7 +87,7 @@ Users.delete_one(filter: {}) Users.delete_many(filter: {}) Users.replace_one(filter: {}, replacement: {}, upsert: false) Users.aggregate(pipeline: []) - +Users.count ``` ## Development diff --git a/lib/mongo_api/base.rb b/lib/mongo_api/base.rb index 1deea0c..9472cf2 100644 --- a/lib/mongo_api/base.rb +++ b/lib/mongo_api/base.rb @@ -90,11 +90,18 @@ def self.size count end + # This method sets the collection name + # + # @return [String] the collection name + def self.collection_name + ancestors.first.to_s.gsub("::", "_").downcase + end + class << self def body { database: ENV["MONGO_DATABASE_NAME"], - collection: ENV["MONGO_COLLECTION_NAME"], + collection: collection_name, dataSource: ENV["MONGO_DATASOURCE_NAME"] } end diff --git a/lib/mongo_api/request.rb b/lib/mongo_api/request.rb index fa3e8fa..5114eb0 100644 --- a/lib/mongo_api/request.rb +++ b/lib/mongo_api/request.rb @@ -175,6 +175,10 @@ def self.count class << self private + # This method is used to parse the response + # + # @param [Block] block + # @return [Hash|Array] the documents array or the response def parse_response(&block) data = block.call diff --git a/lib/mongo_api/version.rb b/lib/mongo_api/version.rb index 626894a..bcdc503 100644 --- a/lib/mongo_api/version.rb +++ b/lib/mongo_api/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MongoApi - VERSION = "0.1.0" + VERSION = "0.1.1" end diff --git a/spec/examples/users_spec.rb b/spec/examples/users_spec.rb index 2a8dd48..f7b28df 100644 --- a/spec/examples/users_spec.rb +++ b/spec/examples/users_spec.rb @@ -9,33 +9,29 @@ RSpec.describe Users do let(:user) { { name: "John Doe", email: "john@gmail.com", type: "DEFAULT" } } + it { expect(Users.collection_name).to eq("users") } it { expect(Users.all).to be_a(Array) } - it { expect(Users.insert(user)).to be_a(Hash) } it "should return a user by name" do response = Users.select(filter: user.slice(:name)).first - expect(response).to be_a(Hash) expect(response["name"]).to eq("John Doe") end it "should update user by email" do set = { name: "John Doe Updated" } response = Users.update(where: user.slice(:email), set:) - expect(response).to be_a(Hash) expect(response["modifiedCount"]).to eq(1) expect(response["matchedCount"]).to eq(1) end it "should delete user by email" do response = Users.destroy(where: user.slice(:email)) - expect(response).to be_a(Hash) expect(response["deletedCount"]).to eq(1) end it "should delete all users" do response = Users.destroy_all - expect(response).to be_a(Hash) expect(response["deletedCount"]).to be >= 0 end end