Skip to content

Commit

Permalink
Merge pull request #110 from amazing-print/ext-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
paddor authored Mar 8, 2024
2 parents 7760939 + b517cf0 commit c56b80c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
10 changes: 2 additions & 8 deletions lib/amazing_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions lib/amazing_print/ext_loader.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions lib/amazing_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,4 +199,4 @@ def merge_custom_defaults!
end
end

# rubocop:enable Metrics/ClassLength
# rubocop:enable Metrics/ClassLength, Metrics/MethodLength

0 comments on commit c56b80c

Please sign in to comment.