Skip to content

Commit

Permalink
Use Cached in Tap.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 23, 2024
1 parent 94e987e commit fe332f8
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 195 deletions.
3 changes: 1 addition & 2 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ def self.default_path(token)
def self.find_cask_in_tap(token, tap)
filename = "#{token}.rb"

Tap.cask_files_by_name(tap)
.fetch(token, tap.cask_dir/filename)
tap.cask_files_by_name.fetch(token, tap.cask_dir/filename)
end
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def check_casktap_integrity
core_cask_tap = CoreCaskTap.instance
return unless core_cask_tap.installed?

broken_tap(core_cask_tap) || examine_git_origin(core_cask_tap.git_repo, core_cask_tap.remote)
broken_tap(core_cask_tap) || examine_git_origin(core_cask_tap.git_repo, T.must(core_cask_tap.remote))
end

sig { returns(T.nilable(String)) }
Expand Down
49 changes: 49 additions & 0 deletions Library/Homebrew/extend/cached.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# typed: strict
# frozen_string_literal: true

module Cached
module Clear
sig { void }
def clear_cache
super if defined?(super)

return unless defined?(@cached_method_calls)

remove_instance_variable(:@cached_method_calls)
end
end

sig { params(method: Symbol).returns(Symbol) }
def cached(method)
uncached_instance_method = instance_method(method)

define_method(method) do |*args, **options, &block|
@cached_method_calls ||= T.let({}, T.nilable(T::Hash[Symbol, T::Hash[T.untyped, T.untyped]]))
cache = @cached_method_calls[method] ||= {}

key = [args, options, block]
if cache.key?(key)
cache.fetch(key)
else
cache[key] = uncached_instance_method.bind(self).call(*args, **options, &block)
end
end
end

sig { params(method: Symbol).returns(Symbol) }
def cached_class_method(method)
uncached_singleton_method = singleton_method(method)

define_singleton_method(method) do |*args, **options, &block|
@cached_method_calls ||= T.let({}, T.nilable(T::Hash[Symbol, T::Hash[T.untyped, T.untyped]]))
cache = @cached_method_calls[method] ||= {}

key = [args, options, block]
if cache.key?(key)
cache[key]
else
cache[key] = uncached_singleton_method.call(*args, **options, &block)
end
end
end
end
9 changes: 9 additions & 0 deletions Library/Homebrew/extend/cached.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# typed: true

module Cached
requires_ancestor { Module }

module Clear
include Kernel
end
end
3 changes: 1 addition & 2 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,6 @@ def self.find_formula_in_tap(name, tap)
"#{name}.rb"
end

Tap.formula_files_by_name(tap)
.fetch(name, tap.formula_dir/filename)
tap.formula_files_by_name.fetch(name, tap.formula_dir/filename)
end
end

0 comments on commit fe332f8

Please sign in to comment.