Module: T::CompatibilityPatches::RSpecCompatibility::MemoizedHelpers

Defined in:
lib/types/compatibility_patches.rb

Instance Method Summary collapse

Instance Method Details

#let(name, &block) ⇒ Object

‘let!`, `subject`, and `subject!` are implemented by dispatching to `let`, so this should cover those methods too.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/types/compatibility_patches.rb', line 62

def let(name, &block)
  # TODO(jez) This will need to use consume!

  current_declaration = T::Private::DeclState.current.active_declaration
  return super unless current_declaration
  T::Private::DeclState.current.reset!

  # Allow the `let`-defined methods to be defined
  super

  # Stash the sig and re-define the method RSpec just defined. This
  # ensures that the `sig` attaches to the outer method, not any
  # method inside the LetDefinitions module.
  #
  # Unfortunately, this means that the runtime checks are not
  # memoized (but they never were).
  #
  # (An alternative approach of pretending that the `sig` was actually
  # written inside the `LetDefinitions` module didn't work, because
  # things like `sig {override}` broke: the `LetDefinitions` module
  # has no ancestors and thus does not override anything)

  method = instance_method(name)
  remove_method(name)
  T::Private::DeclState.current.active_declaration = current_declaration
  define_method(name, method)
end