Module: RSpecMagic::Stable::UseMethodDiscovery::Exports

Defined in:
lib/rspec_magic/stable/use_method_discovery.rb

Instance Method Summary collapse

Instance Method Details

#use_method_discovery(method_let) ⇒ void

Enable the discovery mechanics.

Parameters:

  • method_let (Symbol)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec_magic/stable/use_method_discovery.rb', line 33

def use_method_discovery(method_let)
  # This context and all sub-contexts will respond to A and return B. "Signature" is based on
  # invocation arguments which can vary as we use the feature more intensively. Signature method
  # is the same, thus it shadows higher level definitions completely.
  signature = { method_let: method_let }
  define_singleton_method(:_umd_signature) { signature }

  let(method_let) do
    # NOTE: `self.class` responds to signature method, no need to probe and rescue.
    if (sig = (klass = self.class)._umd_signature) != signature
      raise "`#{method_let}` is shadowed by `#{sig.fetch(:method_let)}` in this context"
    end

    # NOTE: Better not `return` from the loop to keep it debuggable in case logic changes.
    found = nil
    while (klass._umd_signature rescue nil) == signature
      found = self.class.send(:_use_method_discovery_parser, klass.description.to_s) and break
      klass = klass.superclass
    end

    found or raise "No method-like descriptions found to use as `#{method_let}`"
  end
end