@@ -94,34 +94,42 @@ class Binding
94
94
# @return [ String ] A version string for libmongocrypt.
95
95
attach_function :mongocrypt_version , [ :pointer ] , :string
96
96
97
- # Validates if provided version of libmongocrypt is valid, i.e. equal or
98
- # greater than minimum required version. Raises a LoadError if not.
97
+ # Given a string representing a version number, parses it into a
98
+ # Gem::Version object. This handles the case where the string is not
99
+ # in a format supported by Gem::Version by doing some custom parsing.
99
100
#
100
- # @param [ String ] lmc_version String representing libmongocrypt version.
101
+ # @param [ String ] version String representing a version number .
101
102
#
102
- # @raise [ LoadError ] if given version is lesser than minimum required version.
103
+ # @return [ Gem::Version ] the version number
104
+ #
105
+ # @raise [ ArgumentError ] if the string cannot be parsed.
103
106
#
104
107
# @api private
105
- def self . validate_version ( lmc_version )
106
- if ( actual_version = Gem ::Version . new ( lmc_version ) ) < MIN_LIBMONGOCRYPT_VERSION
107
- raise LoadError , "libmongocrypt version #{ MIN_LIBMONGOCRYPT_VERSION } or above is required, " +
108
- "but version #{ actual_version } was found."
109
- end
110
- rescue ArgumentError => e
111
- # Some lmc versions cannot be parsed with Gem::Version class,
112
- # so we fall back to regex.
113
- match = lmc_version . match ( /\A (?<major>\d +)\. (?<minor>\d +)\. (?<patch>\d +)?(-[A-Za-z\+ \d ]+)?\z / )
114
- if match . nil?
115
- raise ArgumentError . new ( "Malformed version number string #{ lmc_version } " )
116
- end
117
- actual_version = Gem ::Version . new (
108
+ def self . parse_version ( version )
109
+ Gem ::Version . new ( version )
110
+ rescue ArgumentError
111
+ match = version . match ( /\A (?<major>\d +)\. (?<minor>\d +)\. (?<patch>\d +)?(-[A-Za-z\+ \d ]+)?\z / )
112
+ raise ArgumentError . new ( "Malformed version number string #{ version } " ) if match . nil?
113
+
114
+ Gem ::Version . new (
118
115
[
119
116
match [ :major ] ,
120
117
match [ :minor ] ,
121
118
match [ :patch ]
122
119
] . join ( '.' )
123
120
)
124
- if actual_version < MIN_LIBMONGOCRYPT_VERSION
121
+ end
122
+
123
+ # Validates if provided version of libmongocrypt is valid, i.e. equal or
124
+ # greater than minimum required version. Raises a LoadError if not.
125
+ #
126
+ # @param [ String ] lmc_version String representing libmongocrypt version.
127
+ #
128
+ # @raise [ LoadError ] if given version is lesser than minimum required version.
129
+ #
130
+ # @api private
131
+ def self . validate_version ( lmc_version )
132
+ if ( actual_version = parse_version ( lmc_version ) ) < MIN_LIBMONGOCRYPT_VERSION
125
133
raise LoadError , "libmongocrypt version #{ MIN_LIBMONGOCRYPT_VERSION } or above is required, " +
126
134
"but version #{ actual_version } was found."
127
135
end
0 commit comments