Extract ruby version checks into a lib/ library

This commit is contained in:
Rick Bradley 2022-08-09 16:00:59 -05:00
Родитель dc8e6b9e1a
Коммит ba8ea8d3c1
9 изменённых файлов: 32 добавлений и 29 удалений

Просмотреть файл

@ -2,12 +2,13 @@
ENV["BUNDLE_GEMFILE"] = File.expand_path("../Gemfile", File.dirname(__FILE__))
require "bundler/setup"
require "ruby-version-check"
# Note that contracts.ruby has two specific ruby-version specific libraries, which we have vendored into lib/
if (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")) # ruby3
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../lib/contracts-ruby3/lib")))
else # ruby2
if RubyVersionCheck.ruby_version2?
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../lib/contracts-ruby2/lib")))
else
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../lib/contracts-ruby3/lib")))
end
require "contracts"

Просмотреть файл

@ -1,29 +1,13 @@
# frozen_string_literal: true
module Entitlements
# Allows maintaining version compatibility with older versions of Ruby
# :nocov:
def self.ruby_version2?
@ruby_version2 ||= (
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0.0") &&
Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0.0")
)
end
def self.ruby_version3?
@ruby_version3 ||= (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0"))
end
# :nocov:
end
# Hey there! With our use of the "contracts" module, load order is important.
# Load third party dependencies first.
require "concurrent"
require "ruby-version-check"
# Note that contracts.ruby has two specific ruby-version specific libraries, which we have vendored into lib/
# :nocov:
if Entitlements.ruby_version2?
if RubyVersionCheck.ruby_version2?
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "contracts-ruby2/lib")))
else
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "contracts-ruby3/lib")))

Просмотреть файл

@ -163,7 +163,7 @@ module Entitlements
# :nocov:
Contract C::None => C::HashOf[String => C::Any]
def parsed_data
@parsed_data ||= if Entitlements.ruby_version2?
@parsed_data ||= if RubyVersionCheck.ruby_version2?
::YAML.load(File.read(filename)).to_h
else
::YAML.load(File.read(filename), permitted_classes: [Date]).to_h

Просмотреть файл

@ -79,7 +79,7 @@ module Entitlements
Entitlements.logger.debug "Loading people from #{filename.inspect}"
# :nocov:
raw_person_data = if Entitlements.ruby_version2?
raw_person_data = if RubyVersionCheck.ruby_version2?
::YAML.load(File.read(filename)).to_h
else
::YAML.load(File.read(filename), permitted_classes: [Date]).to_h

Просмотреть файл

@ -58,7 +58,7 @@ module Entitlements
person_dn_format: config.fetch("person_dn_format")
}
opts[:disable_ssl_verification] = true if config.fetch("disable_ssl_verification", false)
if Entitlements.ruby_version2?
if RubyVersionCheck.ruby_version2?
Entitlements::Service::LDAP.new_with_cache(opts)
else
Entitlements::Service::LDAP.new_with_cache(**opts)

Просмотреть файл

@ -33,7 +33,7 @@ module Entitlements
end
# :nocov:
if Entitlements.ruby_version2?
if RubyVersionCheck.ruby_version2?
::YAML.load(File.read(manager_map_file)).to_h
else
::YAML.load(File.read(manager_map_file), permitted_classes: [Date]).to_h

17
lib/ruby-version-check.rb Normal file
Просмотреть файл

@ -0,0 +1,17 @@
# frozen_string_literal: true
module RubyVersionCheck
# Allows maintaining version compatibility with older versions of Ruby
# :nocov:
def self.ruby_version2?
@ruby_version2 ||= (
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0.0") &&
Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0.0")
)
end
def self.ruby_version3?
@ruby_version3 ||= (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0"))
end
# :nocov:
end

Просмотреть файл

@ -103,7 +103,7 @@ describe Entitlements::Data::Groups::Calculated::Ruby do
it "raises an error when an unexpected data structure is created" do
filename = fixture("ldap-config/filters/filter-bad-data-structure.rb")
if Entitlements.ruby_version2?
if RubyVersionCheck.ruby_version2?
expect { described_class.new(filename: filename) }.to raise_error(ParamContractError)
else
expect { described_class.new(filename: filename) }.to raise_error(ReturnContractError)

Просмотреть файл

@ -1,12 +1,13 @@
# frozen_string_literal: true
require "ruby-version-check"
require "base64"
# Note that contracts.ruby has two specific ruby-version specific libraries, which we have vendored into lib/
if (Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")) # ruby3
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../../lib/contracts-ruby3/lib")))
else # ruby2
if RubyVersionCheck.ruby_version2?
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../../lib/contracts-ruby2/lib")))
else
$LOAD_PATH.unshift(File.expand_path(File.join(__dir__, "../../lib/contracts-ruby3/lib")))
end
require "contracts"