brew/Library/Homebrew/os/mac/version.rb
Jack Nagel c13311ca09 Cache MacOS.version comparison results
MacOS.version#<=> is called many, many times during formula loading with
the same half dozen or so arguments. A typical call to this method
involves:

 * a hash lookup to convert a symbol argument to a string
 * creation of a throw-away Version object wrapping the argument
 * the actual version comparison, which is not cheap

This makes it a prime candidate to be memoized.
2014-04-02 20:29:20 -05:00

49 lines
1.0 KiB
Ruby

require 'version'
module OS
module Mac
class Version < ::Version
SYMBOLS = {
:mavericks => '10.9',
:mountain_lion => '10.8',
:lion => '10.7',
:snow_leopard => '10.6',
:leopard => '10.5',
:tiger => '10.4',
}
def self.from_symbol(sym)
new(SYMBOLS.fetch(sym))
end
def initialize(*args)
super
@comparison_cache = {}
end
def <=>(other)
@comparison_cache.fetch(other) do
v = SYMBOLS.fetch(other, other.to_s)
@comparison_cache[other] = super(Version.new(v))
end
end
def to_sym
case @version
when '10.9' then :mavericks
when '10.8' then :mountain_lion
when '10.7' then :lion
when '10.6' then :snow_leopard
when '10.5' then :leopard
when '10.4' then :tiger
else :dunno
end
end
def pretty_name
to_sym.to_s.split('_').map(&:capitalize).join(' ')
end
end
end
end