lib/rspec/matchers/matcher_delegator.rb in rspec-expectations-3.12.4 vs lib/rspec/matchers/matcher_delegator.rb in rspec-expectations-3.13.0
- old
+ new
@@ -1,9 +1,35 @@
module RSpec
module Matchers
+ # Provides a base class with as little methods as possible, so that
+ # most methods can be delegated via `method_missing`.
+ #
+ # On Ruby 2.0+ BasicObject could be used for this purpose, but it
+ # introduce some extra complexity with constant resolution, so the
+ # BlankSlate pattern was prefered.
+ # @private
+ class BaseDelegator
+ kept_methods = [
+ # Methods that raise warnings if removed.
+ :__id__, :__send__, :object_id,
+
+ # Methods that are explicitly undefined in some subclasses.
+ :==, :===,
+
+ # Methods we keep on purpose.
+ :class, :respond_to?, :__method__, :method, :dup,
+ :clone, :initialize_dup, :initialize_copy, :initialize_clone,
+ ]
+ instance_methods.each do |method|
+ unless kept_methods.include?(method.to_sym)
+ undef_method(method)
+ end
+ end
+ end
+
# Provides the necessary plumbing to wrap a matcher with a decorator.
# @private
- class MatcherDelegator
+ class MatcherDelegator < BaseDelegator
include Composable
attr_reader :base_matcher
def initialize(base_matcher)
@base_matcher = base_matcher