Skip to content

Commit

Permalink
Remove Object#__sortable__ monkey patch, which is only used in Contex…
Browse files Browse the repository at this point in the history
…tual::Memory. Its been moved to private method with a minor amount of refactoring. Existing tests already cover all the functionality.
  • Loading branch information
johnnyshields committed Sep 3, 2023
1 parent 30f46d4 commit b437a66
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 60 deletions.
37 changes: 28 additions & 9 deletions lib/mongoid/contextual/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,22 +627,42 @@ def apply_sorting
end
end

# Compare two values, checking for nil.
# Compare two values, handling the cases when
# either value is nil.
#
# @api private
#
# @example Compare the two objects.
# context.compare(a, b)
#
# @param [ Object ] a The first object.
# @param [ Object ] b The first object.
# @param [ Object ] b The second object.
#
# @return [ Integer ] The comparison value.
def compare(a, b)
case
when a.nil? then b.nil? ? 0 : 1
when b.nil? then -1
else a <=> b
return 0 if a.nil? && b.nil?
return 1 if a.nil?
return -1 if b.nil?

compare_operand(a) <=> compare_operand(b)
end

# Get the operand value to be used in comparison.
# Adds capability to sort boolean values.
#
# @api private
#
# @example Get the comparison operand.
# compare_operand(true) #=> 1
#
# @param [ Object ] value The value to be used in comparison.
#
# @return [ Integer | Object ] The comparison operand.
def compare_operand(value)
case value
when TrueClass then 1
when FalseClass then 0
else value
end
end

Expand All @@ -655,9 +675,8 @@ def compare(a, b)
def in_place_sort(values)
documents.sort! do |a, b|
values.map do |field, direction|
a_value, b_value = a[field], b[field]
direction * compare(a_value.__sortable__, b_value.__sortable__)
end.find { |value| !value.zero? } || 0
direction * compare(a[field], b[field])
end.detect { |value| !value.zero? } || 0
end
end

Expand Down
10 changes: 0 additions & 10 deletions lib/mongoid/extensions/false_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ module Extensions
# Adds type-casting behavior to FalseClass.
module FalseClass

# Get the value of the object as a mongo friendly sort value.
#
# @example Get the object as sort criteria.
# object.__sortable__
#
# @return [ Integer ] 0.
def __sortable__
0
end

# Is the passed value a boolean?
#
# @example Is the value a boolean type?
Expand Down
10 changes: 0 additions & 10 deletions lib/mongoid/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ def __setter__
"#{self}="
end

# Get the value of the object as a mongo friendly sort value.
#
# @example Get the object as sort criteria.
# object.__sortable__
#
# @return [ Object ] self.
def __sortable__
self
end

# Conversion of an object to an $inc-able value.
#
# @example Convert the object.
Expand Down
10 changes: 0 additions & 10 deletions lib/mongoid/extensions/true_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ module Extensions
# Adds type-casting behavior to TrueClass
module TrueClass

# Get the value of the object as a mongo friendly sort value.
#
# @example Get the object as sort criteria.
# object.__sortable__
#
# @return [ Integer ] 1.
def __sortable__
1
end

# Is the passed value a boolean?
#
# @example Is the value a boolean type?
Expand Down
7 changes: 0 additions & 7 deletions spec/mongoid/extensions/false_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

describe Mongoid::Extensions::FalseClass do

describe "#__sortable__" do

it "returns 0" do
expect(false.__sortable__).to eq(0)
end
end

describe "#is_a?" do

context "when provided a Boolean" do
Expand Down
7 changes: 0 additions & 7 deletions spec/mongoid/extensions/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@
end
end

describe "#__sortable__" do

it "returns self" do
expect(object.__sortable__).to eq(object)
end
end

describe ".demongoize" do

let(:object) do
Expand Down
7 changes: 0 additions & 7 deletions spec/mongoid/extensions/true_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

describe Mongoid::Extensions::TrueClass do

describe "#__sortable__" do

it "returns 1" do
expect(true.__sortable__).to eq(1)
end
end

describe "#is_a?" do

context "when provided a Boolean" do
Expand Down

0 comments on commit b437a66

Please sign in to comment.