Improve caching MacOSVersion

This commit is contained in:
Rylan Polster 2025-06-12 23:41:46 -04:00
parent 2e86122164
commit b5fc06cedb
No known key found for this signature in database

View File

@ -2,9 +2,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require "version" require "version"
require "cachable"
# A macOS version. # A macOS version.
class MacOSVersion < Version class MacOSVersion < Version
extend Cachable
# Raised when a macOS version is unsupported. # Raised when a macOS version is unsupported.
class Error < RuntimeError class Error < RuntimeError
sig { returns(T.nilable(T.any(String, Symbol))) } sig { returns(T.nilable(T.any(String, Symbol))) }
@ -47,8 +50,10 @@ class MacOSVersion < Version
sig { params(version: Symbol).returns(T.attached_class) } sig { params(version: Symbol).returns(T.attached_class) }
def self.from_symbol(version) def self.from_symbol(version)
str = SYMBOLS.fetch(version) { raise MacOSVersion::Error, version } cache.fetch(version) do |v|
new(str) str = SYMBOLS.fetch(v) { raise MacOSVersion::Error, v }
cache[v] = new(str)
end
end end
sig { params(version: T.nilable(String)).void } sig { params(version: T.nilable(String)).void }
@ -62,7 +67,7 @@ class MacOSVersion < Version
sig { override.params(other: T.untyped).returns(T.nilable(Integer)) } sig { override.params(other: T.untyped).returns(T.nilable(Integer)) }
def <=>(other) def <=>(other)
return @comparison_cache[other] if @comparison_cache.key?(other) return @comparison_cache[other.to_s] if @comparison_cache.key?(other.to_s)
result = case other result = case other
when Symbol when Symbol
@ -76,7 +81,7 @@ class MacOSVersion < Version
super super
end end
@comparison_cache[other] = result unless frozen? @comparison_cache[other.to_s] = result unless frozen?
result result
end end