Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[READY FOR REVIEW] MONGOID-5702: Monkey Patch Removal: Remove __numeric__ class method from Numeric module #5746

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
18 changes: 3 additions & 15 deletions lib/mongoid/criteria/queryable/extensions/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ def __evolve_time__

module ClassMethods

# Get the object as a numeric.
#
# @api private
#
# @example Get the object as numeric.
# Object.__numeric__("1.442")
#
# @param [ Object ] object The object to convert.
#
# @return [ Object ] The converted number.
def __numeric__(object)
object.to_s.match?(/\A[-+]?[0-9]*[0-9.]0*\z/) ? object.to_i : Float(object)
end

# Evolve the object to an integer.
#
# @example Evolve to integers.
Expand All @@ -57,7 +43,9 @@ def __numeric__(object)
# @return [ Integer ] The evolved object.
def evolve(object)
__evolve__(object) do |obj|
__numeric__(obj) rescue obj
obj.to_s.match?(/\A[-+]?[0-9]*[0-9.]0*\z/) ? obj.to_i : Float(obj)
rescue
obj
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions spec/mongoid/criteria/queryable/extensions/numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

let(:host) do
Class.new do
include Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods
end.new
extend Mongoid::Criteria::Queryable::Extensions::Numeric::ClassMethods
end
end

describe "#__numeric__" do
let(:actual) { host.__numeric__(str) }
describe ".evolve" do
let(:actual) { host.evolve(str) }

context "when the string is a whole number" do
let(:str) { '123' }
Expand Down Expand Up @@ -80,40 +80,40 @@
context "when the string is non-numeric" do
let(:str) { 'foo' }

it "raises ArgumentError" do
expect { actual }.to raise_error(ArgumentError)
it "returns the string" do
expect(actual).to eq(str)
end
end

context "when the string is non-numeric with leading dot" do
let(:str) { '.foo' }

it "raises ArgumentError" do
expect { actual }.to raise_error(ArgumentError)
it "returns the string" do
expect(actual).to eq(str)
end
end

context "when the string is non-numeric with trailing dot" do
let(:str) { 'foo.' }

it "raises ArgumentError" do
expect { actual }.to raise_error(ArgumentError)
it "returns the string" do
expect(actual).to eq(str)
end
end

context "when the string is non-numeric with trailing dot and zeroes" do
let(:str) { 'foo.000' }

it "raises ArgumentError" do
expect { actual }.to raise_error(ArgumentError)
it "returns the string" do
expect(actual).to eq(str)
end
end

context "when the string is empty" do
let(:str) { '' }

it "raises ArgumentError" do
expect { actual }.to raise_error(ArgumentError)
it "returns the string" do
expect(actual).to eq(str)
end
end
end
Expand Down
Loading