Inline index_by

This commit is contained in:
Douglas Eichelberger 2024-01-22 10:03:17 -08:00
parent df140b329f
commit 2f8ad2f5e6
2 changed files with 7 additions and 24 deletions

View File

@ -2,24 +2,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Enumerable module Enumerable
# Convert an enumerable to a hash, using the block result as the key and the
# element as the value.
#
# people.index_by(&:login)
# # => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
#
# people.index_by { |person| "#{person.first_name} #{person.last_name}" }
# # => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
def index_by(&block)
if block
result = {}
each { |elem| result[yield(elem)] = elem }
result
else
T.unsafe(self).to_enum(:index_by) { T.unsafe(self).size if respond_to?(:size) }
end
end
# The negative of the <tt>Enumerable#include?</tt>. Returns +true+ if the # The negative of the <tt>Enumerable#include?</tt>. Returns +true+ if the
# collection does not include the object. # collection does not include the object.
sig { params(object: T.untyped).returns(T::Boolean) } sig { params(object: T.untyped).returns(T::Boolean) }

View File

@ -565,8 +565,8 @@ on_request: installed_on_request?, options: options)
def expand_requirements def expand_requirements
unsatisfied_reqs = Hash.new { |h, k| h[k] = [] } unsatisfied_reqs = Hash.new { |h, k| h[k] = [] }
formulae = [formula] formulae = [formula]
formula_deps_map = formula.recursive_dependencies formula_deps_map = {}
.index_by(&:name) formula.recursive_dependencies.each { |dep| formula_deps_map[dep.name] = dep }
while (f = formulae.pop) while (f = formulae.pop)
runtime_requirements = runtime_requirements(f) runtime_requirements = runtime_requirements(f)
@ -1206,10 +1206,11 @@ on_request: installed_on_request?, options: options)
def fetch_bottle_tab def fetch_bottle_tab
@fetch_bottle_tab ||= begin @fetch_bottle_tab ||= begin
formula.fetch_bottle_tab formula.fetch_bottle_tab
@bottle_tab_runtime_dependencies = formula.bottle_tab_attributes @bottle_tab_runtime_dependencies = {}
formula.bottle_tab_attributes
.fetch("runtime_dependencies", []) .fetch("runtime_dependencies", [])
.index_by { |dep| dep["full_name"] } .each { |dep| @bottle_tab_runtime_dependencies[dep["full_name"]] = dep }
.freeze @bottle_tab_runtime_dependencies.freeze
true true
rescue DownloadError, ArgumentError rescue DownloadError, ArgumentError
@fetch_bottle_tab = true @fetch_bottle_tab = true