Skip to content

Commit b5e6c0e

Browse files
committed
Add Overcommit::Utils::Version helper class
This will be used in an upcoming commit to support easy switching of behavior based on git version. It piggybacks on `Gem::Version` to give us easy version comparisons without any effort on our part, and defines a shortcut helper so we don't need to explicitly convert version strings to objects in order to compare.
1 parent 61e5d36 commit b5e6c0e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/overcommit/utils.rb

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
module Overcommit
55
# Utility functions for general use.
66
module Utils
7+
# Helper class for doing quick constraint validations on version numbers.
8+
#
9+
# This allows us to execute code based on the git version.
10+
class Version < Gem::Version
11+
# Overload comparison operators so we can conveniently compare this
12+
# version directly to a string in code.
13+
%w[< <= > >= == !=].each do |operator|
14+
define_method operator do |version|
15+
case version
16+
when String
17+
super(Gem::Version.new(version))
18+
else
19+
super(version)
20+
end
21+
end
22+
end
23+
end
24+
725
class << self
826
def script_path(script)
927
File.join(OVERCOMMIT_HOME, 'libexec', script)

0 commit comments

Comments
 (0)