From b517cf0027d159a046afd7f0486dfa809c93562d Mon Sep 17 00:00:00 2001 From: Kevin McCormack Date: Thu, 7 Mar 2024 22:42:36 -0500 Subject: [PATCH] Add ExtLoader This should help with instances where libraries are required before their extensions resulting in the extensions never getting loaded. --- lib/amazing_print.rb | 10 ++-------- lib/amazing_print/ext_loader.rb | 27 +++++++++++++++++++++++++++ lib/amazing_print/inspector.rb | 7 +++++-- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 lib/amazing_print/ext_loader.rb diff --git a/lib/amazing_print.rb b/lib/amazing_print.rb index 9444f00..df08089 100644 --- a/lib/amazing_print.rb +++ b/lib/amazing_print.rb @@ -33,12 +33,6 @@ require_relative 'amazing_print/ext/action_view' end end - require_relative 'amazing_print/ext/mongo_mapper' if defined?(MongoMapper) - require_relative 'amazing_print/ext/mongoid' if defined?(Mongoid) - require_relative 'amazing_print/ext/nokogiri' if defined?(Nokogiri) - require_relative 'amazing_print/ext/nobrainer' if defined?(NoBrainer) - require_relative 'amazing_print/ext/ripple' if defined?(Ripple) - require_relative 'amazing_print/ext/sequel' if defined?(Sequel) - require_relative 'amazing_print/ext/ostruct' if defined?(OpenStruct) + + AmazingPrint::ExtLoader.call end -# test diff --git a/lib/amazing_print/ext_loader.rb b/lib/amazing_print/ext_loader.rb new file mode 100644 index 0000000..bcb7856 --- /dev/null +++ b/lib/amazing_print/ext_loader.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module AmazingPrint + ## + # Attempt to load extensions up to 3 times since this library may be required + # before dependencies that we have extensions for. + # + class ExtLoader + EXT_LOAD_ATTEMPT_LIMIT = 3 + + @load_attemps = 0 + + def self.call + return if @load_attemps >= EXT_LOAD_ATTEMPT_LIMIT + + require_relative 'ext/mongo_mapper' if defined?(MongoMapper) + require_relative 'ext/mongoid' if defined?(Mongoid) + require_relative 'ext/nobrainer' if defined?(NoBrainer) + require_relative 'ext/nokogiri' if defined?(Nokogiri) + require_relative 'ext/ostruct' if defined?(OpenStruct) # rubocop:disable Style/OpenStructUse + require_relative 'ext/ripple' if defined?(Ripple) + require_relative 'ext/sequel' if defined?(Sequel) + + @load_attemps += 1 + end + end +end diff --git a/lib/amazing_print/inspector.rb b/lib/amazing_print/inspector.rb index e89e890..4cd9bad 100644 --- a/lib/amazing_print/inspector.rb +++ b/lib/amazing_print/inspector.rb @@ -6,9 +6,10 @@ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ -# rubocop:disable Metrics/ClassLength +# rubocop:disable Metrics/ClassLength, Metrics/MethodLength require_relative 'indentator' +require_relative 'ext_loader' module AmazingPrint class Inspector @@ -70,6 +71,8 @@ def initialize(options = {}) @formatter = AmazingPrint::Formatter.new(self) @indentator = AmazingPrint::Indentator.new(@options[:indent].abs) Thread.current[AP] ||= [] + + ExtLoader.call end def current_indentation @@ -196,4 +199,4 @@ def merge_custom_defaults! end end -# rubocop:enable Metrics/ClassLength +# rubocop:enable Metrics/ClassLength, Metrics/MethodLength