Sha256: 18e3fcbb84a0e4046b21d7d8058901e0d53874dc86c38acbb71ac868c51204cb

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

module PropCheck
  class Property
    ##
    # A wrapper class that implements the 'Cloaker' concept
    # which allows us to refer to variables set in 'bindings',
    # while still being able to access things that are only in scope
    # in the creator of '&block'.
    #
    # This allows us to bind the variables specified in `bindings`
    # one way during checking and another way during shrinking.
    class CheckEvaluator
      include RSpec::Matchers if Object.const_defined?('RSpec')

      def initialize(bindings, &block)
        @caller = block.binding.receiver
        @block = block
        define_named_instance_methods(bindings)
      end

      def call
        self.instance_exec(&@block)
      end

      private def define_named_instance_methods(results)
        results.each do |name, result|
          define_singleton_method(name) { result }
        end
      end

      ##
      # Dispatches to caller whenever something is not part of `bindings`.
      # (No need to invoke this method manually)
      def method_missing(method, *args, &block)
        @caller.__send__(method, *args, &block) || super
      end

      ##
      # Checks respond_to of caller whenever something is not part of `bindings`.
      # (No need to invoke this method manually)
      def respond_to_missing?(*args)
        @caller.respond_to?(*args) || super
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
prop_check-0.7.1 lib/prop_check/property/check_evaluator.rb
prop_check-0.7.0 lib/prop_check/property/check_evaluator.rb
prop_check-0.6.2 lib/prop_check/property/check_evaluator.rb
prop_check-0.6.1 lib/prop_check/property/check_evaluator.rb
prop_check-0.6.0 lib/prop_check/property/check_evaluator.rb