Class: Brine::Selecting::Selector

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

Overview

Allow selection of 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

Constant Summary

Constants included from ParameterTransforming

ParameterTransforming::DATE, ParameterTransforming::MILLIS, ParameterTransforming::TIME, ParameterTransforming::TZ

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParameterTransforming

#expand, #parameter_transformers, #transformed_parameter

Constructor Details

#initialize(target, negated = false) ⇒ Selector

Construct a selector to perform assertions against a provided target.

Parameters:

  • target (Object)

    Provide the value against which assertions will be performed.

  • negated (Boolean) (defaults to: false)

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



42
43
44
45
# File 'lib/brine/selecting.rb', line 42

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

Instance Attribute Details

#coercerObject

Coercer

Expose the Coercer that may modify values prior to performing assertions.



33
34
35
# File 'lib/brine/selecting.rb', line 33

def coercer
  @coercer
end

Instance Method Details

#assert_that(value, binding, negated = nil) ⇒ Object

Perform the provided assertion against the instance target.

The values will be coerced prior to evaluation.

Parameters:

  • value (Object)

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

  • binding (Object)

    Provide the binding environment which will be used to expand any templates

  • negated (Boolean) (defaults to: nil)

    Specify whether the assertion should be expected to fail, if false it should pass.

  • Define (Block)

    the logic 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.



73
74
75
76
77
78
79
80
# File 'lib/brine/selecting.rb', line 73

def assert_that(value, binding, negated=nil)
  # shim while moving negation to assertions.
  negated = @negated if negated.nil?
  target, value = coercer.coerce(expand(@target, binding), expand(value, binding))
  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. The default implementation is a passthrough.

Parameters:

  • matcher (RSpec::Matcher)

    Relay the matcher originally passed to #assert_that.

Returns:

  • (RSpec::Matcher)

    Return the Matcher to use while performing the assertion.



57
58
59
# File 'lib/brine/selecting.rb', line 57

def filter_matcher(matcher)
  matcher
end