-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Builder class to manipulate args into docs formatted for driver API
- Loading branch information
Showing
6 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'mongo' | ||
require 'rom/core' | ||
|
||
require 'rom/mongo/version' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module ROM | ||
module Mongo | ||
module Builder | ||
|
||
SORT_DIRECTION_MAP = { | ||
asc: ::Mongo::Index::ASCENDING, | ||
desc: ::Mongo::Index::DESCENDING, | ||
} | ||
|
||
extend self | ||
|
||
def to_projection(fields) | ||
fields.inject({}) do |doc, f| | ||
doc.merge!(f => 1) | ||
end | ||
end | ||
|
||
|
||
def to_sort_doc(doc) | ||
doc.inject({}) do |sort_doc, (key, value)| | ||
if SORT_DIRECTION_MAP.values.include?(value) | ||
sort_doc.merge!(key => value) | ||
elsif direction = SORT_DIRECTION_MAP[value] | ||
sort_doc.merge!(key => direction) | ||
else | ||
sort_doc | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
require 'mongo' | ||
require 'uri' | ||
|
||
require 'rom/gateway' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe ROM::Mongo::Builder do | ||
|
||
describe '#to_projection' do | ||
|
||
let(:result) do | ||
described_class.to_projection([:name, :address]) | ||
end | ||
|
||
it 'converts the fields into a document with 1 values' do | ||
expect(result).to eq({ name: 1, address: 1}) | ||
end | ||
end | ||
|
||
describe '#to_sort_doc' do | ||
|
||
let(:result) do | ||
described_class.to_sort_doc(arg) | ||
end | ||
|
||
context 'when symbols are used as the direction' do | ||
|
||
let(:arg) do | ||
{name: :asc, address: :desc} | ||
end | ||
|
||
it 'converts the fields into a document with 1 values' do | ||
expect(result).to eq({ name: 1, address: -1 }) | ||
end | ||
end | ||
|
||
context 'when integers are used as the direction' do | ||
|
||
let(:arg) do | ||
{name: 1, address: -1} | ||
end | ||
|
||
it 'converts the fields into a document with 1 values' do | ||
expect(result).to eq({ name: 1, address: -1 }) | ||
end | ||
end | ||
|
||
context 'when the direction is invalid' do | ||
|
||
let(:arg) do | ||
{name: 1, address: :invalid} | ||
end | ||
|
||
it 'ignores the invalid direction' do | ||
expect(result).to eq({ name: 1 }) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters