Skip to content

Commit

Permalink
MONGOID-4906 Implement $mod (#4922)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksadoff authored Nov 18, 2020
1 parent 3839fc4 commit 6800f70
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/mongoid/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ module Matcher
require 'mongoid/matcher/in'
require 'mongoid/matcher/lt'
require 'mongoid/matcher/lte'
require 'mongoid/matcher/mod'
require 'mongoid/matcher/ne'
require 'mongoid/matcher/nin'
require 'mongoid/matcher/nor'
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/matcher/field_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module FieldOperator
'$in' => In,
'$lt' => Lt,
'$lte' => Lte,
'$mod' => Mod,
'$nin' => Nin,
'$ne' => Ne,
'$not' => Not,
Expand Down
17 changes: 17 additions & 0 deletions lib/mongoid/matcher/mod.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Mongoid
module Matcher

# @api private
module Mod
module_function def matches?(exists, value, condition)
unless Array === condition
raise Errors::InvalidQuery, "Unknown $mod argument #{condition}"
end
if condition.length != 2
raise Errors::InvalidQuery, "Malformed $mod argument #{condition}, should have 2 elements"
end
condition[1] == value%condition[0]
end
end
end
end
55 changes: 55 additions & 0 deletions spec/integration/matcher_operator_data/mod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
- name: existing field - matches
document:
pi: 8
query:
pi:
$mod: [5, 3]
matches: true

- name: existing field - does not match
document:
pi: 9
query:
pi:
$mod: [5, 3]
matches: false

- name: invalid condition
document:
pi: 9
query:
pi:
$mod: 5
error: true

- name: invalid field
document:
pi: [1, 2]
query:
pi:
$mod: 5
error: true

- name: array too short
document:
pi: 3
query:
pi:
$mod: [5]
error: true

- name: empty array
document:
pi: 3
query:
pi:
$mod: []
error: true

- name: array too long
document:
pi: 3
query:
pi:
$mod: [1, 2, 3]
error: true

0 comments on commit 6800f70

Please sign in to comment.