Skip to content

Commit

Permalink
Refactor Ruby platform priority condition to its own method
Browse files Browse the repository at this point in the history
The `Gem::Platform::RUBY ? -1 : 1` has been used multiple times in different places and could be refactored to a method (DRY).
  • Loading branch information
daniel-niknam committed Aug 19, 2021
1 parent 5b910cc commit 9d43ca8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/rubygems/name_tuple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ def inspect # :nodoc:
alias to_s inspect # :nodoc:

def <=>(other)
[@name, @version, @platform == Gem::Platform::RUBY ? -1 : 1] <=>
[other.name, other.version,
other.platform == Gem::Platform::RUBY ? -1 : 1]
[@name, @version, Gem::Platform.sort_priority(@platform)] <=>
[other.name, other.version, Gem::Platform.sort_priority(other.platform)]
end

include Comparable
Expand Down
4 changes: 4 additions & 0 deletions lib/rubygems/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def self.match_gem?(platform, gem_name)
match_platforms?(platform, Gem.platforms)
end

def self.sort_priority(platform)
platform == Gem::Platform::RUBY ? -1 : 1
end

def self.installable?(spec)
if spec.respond_to? :installable_platform?
spec.installable_platform?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/resolver/installer_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def add_always_install(dependency)
end

found = found.sort_by do |s|
[s.version, s.platform == Gem::Platform::RUBY ? -1 : 1]
[s.version, Gem::Platform.sort_priority(s.platform)]
end

newest = found.last
Expand Down
4 changes: 2 additions & 2 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ def self._resort!(specs) # :nodoc:
next names if names.nonzero?
versions = b.version <=> a.version
next versions if versions.nonzero?
b.platform == Gem::Platform::RUBY ? -1 : 1
Gem::Platform.sort_priority(b.platform)
end
end

Expand Down Expand Up @@ -2333,7 +2333,7 @@ def satisfies_requirement?(dependency)
# Returns an object you can use to sort specifications in #sort_by.

def sort_obj
[@name, @version, @new_platform == Gem::Platform::RUBY ? -1 : 1]
[@name, @version, Gem::Platform.sort_priority(@new_platform)]
end

##
Expand Down

0 comments on commit 9d43ca8

Please sign in to comment.