Class: RSpec::Matchers::AliasedMatcher Private

Inherits:
MatcherDelegator
  • Object
show all
Defined in:
lib/rspec/matchers/aliased_matcher.rb

Overview

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

Decorator that wraps a matcher and overrides description using the provided block in order to support an alias of a matcher. This is intended for use when composing matchers, so that you can use an expression like include( a_value_within(0.1).of(3) ) rather than include( be_within(0.1).of(3) ), and have the corresponding description read naturally.

Instance Method Summary (collapse)

Constructor Details

- (AliasedMatcher) initialize(base_matcher, description_block)

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.

Returns a new instance of AliasedMatcher

13
14
15
16
# File 'lib/rspec/matchers/aliased_matcher.rb', line 13
def initialize(base_matcher, description_block)
  @description_block = description_block
  super(base_matcher)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing

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.

Forward messages on to the wrapped matcher. Since many matchers provide a fluent interface (e.g. a_value_within(0.1).of(3)), we need to wrap the returned value if it responds to description, so that our override can be applied when it is eventually used.

24
25
26
27
28
# File 'lib/rspec/matchers/aliased_matcher.rb', line 24
def method_missing(*)
  return_val = super
  return return_val unless return_val.respond_to?(:description)
  AliasedMatcher.new(return_val, @description_block)
end

Instance Method Details

- (Object) description

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.

Provides the description of the aliased matcher. Aliased matchers are designed to behave identically to the original matcher except for this method. The description is different to reflect the aliased name.

36
37
38
# File 'lib/rspec/matchers/aliased_matcher.rb', line 36
def description
  @description_block.call(super)
end