Class: Brine::Selecting::Selector

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/brine/selecting.rb

Overview

An object responsible for selecting one or more values. This Selector will test whether the targeted value itself satisfies the assertion.

RSpec is used within this implementation to perform assertions. The Selector ultimately perform this assertion by accepting an RSpec matcher which it applied against the targeted value.

Direct Known Subclasses

AllSelector, AnySelector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, negated = false) ⇒ Selector

Construct a selector to perform assertions against a provided target.

Parameters:

  • taret (Object)

    The value against which assertions will be performed.

  • negated (Boolean) (defaults to: false)

    Whether the assertions from this selector should be negated. This is deprecated and should instead be passed to #assert_that.



40
41
42
43
# File 'lib/brine/selecting.rb', line 40

def initialize(target, negated=false)
  @target = target
  @negated = negated
end

Instance Attribute Details

#coercerObject

Coercer

The Coercer that may modify values prior to performing assertions.



31
32
33
# File 'lib/brine/selecting.rb', line 31

def coercer
  @coercer
end

Instance Method Details

#assert_that(value, negated = nil) ⇒ Object

Perform the provided assertion against the instance target.

The values will be coerced prior to evaluation.

Parameters:

  • value (Object)

    The value/parameter against which the target will be compared. In cases where no parameter is needed for comparison, this may be nil.

  • negated (Boolean) (defaults to: nil)

    If true the assertion should be expected to fail, otherwise it should pass.

  • A (Block)

    block which will be passed a coerced copy of ‘value` and which should return an RSpec matcher which will be evaluated against the coerced target value.



69
70
71
72
73
74
75
76
# File 'lib/brine/selecting.rb', line 69

def assert_that(value, negated=nil)
  # shim while moving negation to assertions.
  negated = @negated if negated.nil?
  target, value = coercer.coerce(@target, value)
  message = negated ? :to_not : :to
  matcher = filter_matcher(yield(value))
  expect(target).send(message, matcher)
end

#filter_matcher(matcher) ⇒ RSpec::Matcher

Optionally perform some modification to the RSpec matcher prior to assertion.

This is designed to allow subclassess to be able to modify the way in which matchers are applied against the values. This method is a pass-through in this class.

Parameters:

  • matcher (RSpec::Matcher)

    The matcher originally passed to #assert_that.

Returns:

  • (RSpec::Matcher)

    The Matcher to be used while performing the assertion.



54
55
56
# File 'lib/brine/selecting.rb', line 54

def filter_matcher(matcher)
  matcher
end