Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExtLoader #110

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading