Commit 0ec5b7ac by Bruno Buccolo

Adds #start and #mstart matchers

parent c5847b7f
...@@ -56,6 +56,8 @@ end ...@@ -56,6 +56,8 @@ end
* not_eq * not_eq
* cont * cont
* in * in
* start
* mstart
* gt * gt
* lt * lt
* gteq * gteq
......
module RansackMongo module RansackMongo
class MongoAdapter class MongoAdapter
PREDICATES = %w[eq not_eq cont in gt lt gteq lteq] PREDICATES = %w[eq not_eq cont in start mstart gt lt gteq lteq]
def initialize def initialize
@query = {} @query = {}
...@@ -26,6 +26,20 @@ module RansackMongo ...@@ -26,6 +26,20 @@ module RansackMongo
@query[attr] = { '$in' => value } @query[attr] = { '$in' => value }
end end
def start_matcher(attr, value)
@query[attr] = { '$in' => [/^#{value}/] }
end
def mstart_matcher(attr, value)
values = value.split(",").map do |current|
if (current = current.strip).length > 0
/^#{current}/
end
end.compact
@query[attr] = { '$in' => values }
end
def gt_matcher(attr, value) def gt_matcher(attr, value)
append_sizeable_matcher('$gt', attr, value) append_sizeable_matcher('$gt', attr, value)
end end
......
...@@ -34,6 +34,34 @@ module RansackMongo ...@@ -34,6 +34,34 @@ module RansackMongo
end end
end end
describe '#start' do
it 'returns the matcher' do
subject.start_matcher('object_ref', 'Bruno')
expect(subject.to_query).to eq("object_ref" => { "$in" => [/^Bruno/] })
end
end
describe '#mstart' do
it 'returns the matcher' do
subject.mstart_matcher('name', 'Pablo, Bruno,Dude')
expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/, /^Bruno/, /^Dude/] })
end
it 'cleans up the input' do
subject.mstart_matcher('name', ',, , ,Pablo,,,, ,, , , ,')
expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/] })
end
it 'accepts single values' do
subject.mstart_matcher('name', 'Pablo')
expect(subject.to_query).to eq("name" => { "$in" => [/^Pablo/] })
end
end
context 'when combine gt lt gteq and lteq' do context 'when combine gt lt gteq and lteq' do
it 'returns all matchers' do it 'returns all matchers' do
subject.gt_matcher('count', '1') subject.gt_matcher('count', '1')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment