Skip to content

Commit

Permalink
Use #flat_map on Ruby 2.0 +
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Jul 25, 2017
1 parent f30e420 commit 028f16a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/molinillo.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'molinillo/compatibility'
require 'molinillo/gem_metadata'
require 'molinillo/errors'
require 'molinillo/resolver'
Expand Down
26 changes: 26 additions & 0 deletions lib/molinillo/compatibility.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Molinillo
# Hacks needed for old Ruby versions.
module Compatibility
module_function

if [].respond_to?(:flat_map)
# Flat map
# @param [Enumerable] enum an enumerable object
# @block the block to flat-map with
# @return The enum, flat-mapped
def flat_map(enum, &blk)
enum.flat_map(&blk)
end
else
# Flat map
# @param [Enumerable] enum an enumerable object
# @block the block to flat-map with
# @return The enum, flat-mapped
def flat_map(enum, &blk)
enum.map(&blk).flatten(1)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/molinillo/dependency_graph/vertex.rb
Expand Up @@ -54,7 +54,7 @@ def predecessors
# {#descendent?}
def recursive_predecessors
vertices = predecessors
vertices += vertices.map(&:recursive_predecessors).flatten(1)
vertices += Compatibility.flat_map(vertices, &:recursive_predecessors)
vertices.uniq!
vertices
end
Expand All @@ -69,7 +69,7 @@ def successors
# {#ancestor?}
def recursive_successors
vertices = successors
vertices += vertices.map(&:recursive_successors).flatten(1)
vertices += Compatibility.flat_map(vertices, &:recursive_successors)
vertices.uniq!
vertices
end
Expand Down
2 changes: 1 addition & 1 deletion lib/molinillo/errors.rb
Expand Up @@ -65,7 +65,7 @@ class VersionConflict < ResolverError
# @param [SpecificationProvider] specification_provider see {#specification_provider}
def initialize(conflicts, specification_provider)
pairs = []
conflicts.values.flatten.map(&:requirements).flatten.each do |conflicting|
Compatibility.flat_map(conflicts.values.flatten, &:requirements).each do |conflicting|
conflicting.each do |source, conflict_requirements|
conflict_requirements.each do |c|
pairs << [c, source]
Expand Down

0 comments on commit 028f16a

Please sign in to comment.