Skip to content
Open
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
6 changes: 6 additions & 0 deletions lib/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

require 'pact/railtie' if defined?(Rails::Railtie)

# Load core_ext polyfills (blank?/present?, deep_dup) before Zeitwerk eager-loads
# the rest of the gem. Each polyfill is skipped if already defined by active_support
# or any other library loaded by the host application.
require 'pact/support/core_ext'

module Pact
class Error < StandardError; end

Expand Down Expand Up @@ -41,6 +46,7 @@ def self.configuration
loader.ignore("#{__dir__}/pact/version.rb")
loader.ignore("#{__dir__}/pact/rspec.rb")
loader.ignore("#{__dir__}/pact/rspec")
loader.ignore("#{__dir__}/pact/support/core_ext.rb")
loader.ignore("#{__dir__}/pact/railtie.rb") unless defined?(Rails::Railtie)

loader.setup
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/rspec/support/webmock/webmock_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module WebmockHelpers
def self.turned_off
yield unless defined?(::WebMock)
return yield unless defined?(::WebMock)

allow_net_connect = WebMock::Config.instance.allow_net_connect
allow_localhost = WebMock::Config.instance.allow_localhost
Expand Down
93 changes: 93 additions & 0 deletions lib/pact/support/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# frozen_string_literal: true

# Minimal active_support polyfills for blank?/present? and deep_dup.
# Each block is skipped entirely if the method is already defined
# (e.g. active_support is loaded by the host application).

unless Object.method_defined?(:blank?)
class NilClass
def blank? = true
def present? = false
end

class FalseClass
def blank? = true
def present? = false
end

class TrueClass
def blank? = false
def present? = true
end

class String
BLANK_RE = /\A[[:space:]]*\z/

def blank?
empty? || BLANK_RE.match?(self)
end

def present? = !blank?
end

class Symbol
alias_method :blank?, :empty?
def present? = !blank?
end

class Array
alias_method :blank?, :empty?
def present? = !empty?
end

class Hash
alias_method :blank?, :empty?
def present? = !empty?
end

class Object
def blank? = respond_to?(:empty?) ? !!empty? : false
def present? = !blank?
Comment thread
YOU54F marked this conversation as resolved.

def presence
self if present?
end
end
end

unless Object.method_defined?(:deep_dup)
class Object
def deep_dup
dup
rescue TypeError
self
end
end

class Array
def deep_dup
map(&:deep_dup)
end
end

class Hash
def deep_dup
hash = dup
each_pair do |key, value|
if ::String === key || ::Symbol === key
hash[key] = value.deep_dup
else
hash.delete(key)
hash[key.deep_dup] = value.deep_dup
end
end
hash
end
end

class Module
def deep_dup
name.nil? ? super : self
end
end
end
6 changes: 6 additions & 0 deletions pact.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Gem::Specification.new do |gem|
'documentation_uri' => 'https://github.com/pact-foundation/pact-ruby/blob/master/README.md'
}

# Optional runtime dependencies (not required by pact itself):
# active_support — if already loaded, its blank?/present? take precedence over
# pact's built-in polyfill (lib/pact/support/blank.rb).
# webmock — only needed for WebMock integration via WebmockHelpers.
# Require it yourself before using that helper.

# Core dependencies (code loading)
gem.add_dependency 'zeitwerk', '~> 2.3'
# For Pact support via Pact Rust Core
Expand Down
Loading