Module: RSpec::Maybes::Syntax Private

Defined in:
lib/rspec/maybes/syntax.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provides methods for enabling and disabling the maybe syntax

Constant Summary collapse

MONKEYPATCHED_CLASSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  RSpec::Matchers::BuiltIn::BaseMatcher,
  RSpec::Matchers::BuiltIn::RaiseError,
  RSpec::Matchers::BuiltIn::ThrowSymbol
]

Class Method Summary collapse

Class Method Details

.disable_maybe(syntax_host = ::RSpec::Matchers) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Disables the `maybe` syntax.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec/maybes/syntax.rb', line 48

def disable_maybe(syntax_host = ::RSpec::Matchers)
  return unless maybe_enabled?(syntax_host)

  syntax_host.module_exec do
    undef maybe
  end

  MONKEYPATCHED_CLASSES.each do |klass|
    klass.class_eval do
      undef matches?
      alias matches? old_matches?
      undef old_matches?

      undef on_my_machine
      undef on_your_machine?
    end
  end
end

.enable_maybe(syntax_host = ::RSpec::Matchers) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enables the `maybe` syntax.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec/maybes/syntax.rb', line 18

def enable_maybe(syntax_host = ::RSpec::Matchers)
  return if maybe_enabled?(syntax_host)

  syntax_host.module_exec do
    def maybe(value = ::RSpec::Maybes::MaybeTarget::UndefinedValue, &block)
      ::RSpec::Maybes::MaybeTarget.for(value, block)
    end
  end

  MONKEYPATCHED_CLASSES.each do |klass|
    klass.class_eval do
      alias old_matches? matches?

      def matches?(actual)
        @actual = actual
        @your_machine || old_matches?(actual)
      end

      def on_my_machine
        @your_machine = true
        return self
      end

      def on_your_machine?() @your_machine end
    end
  end
end

.maybe_enabled?(syntax_host = ::RSpec::Matchers) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates whether or not the `maybe` syntax is enabled.

Returns:

  • (Boolean)

69
70
71
# File 'lib/rspec/maybes/syntax.rb', line 69

def maybe_enabled?(syntax_host = ::RSpec::Matchers)
  syntax_host.method_defined?(:maybe)
end