Skip to content

Commit

Permalink
Merge pull request #2 from nemuba/v0.1.1
Browse files Browse the repository at this point in the history
V0.1.1
  • Loading branch information
nemuba authored Oct 12, 2023
2 parents cb3f6b6 + 4affbc8 commit 737f978
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
mongo_api (0.1.0)
mongo_api (0.1.1)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/mongo_api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/mongo_api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MongoApi
VERSION = "0.1.0"
VERSION = "0.1.1"
end
6 changes: 1 addition & 5 deletions spec/examples/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,29 @@
RSpec.describe Users do
let(:user) { { name: "John Doe", email: "[email protected]", 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

0 comments on commit 737f978

Please sign in to comment.