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.



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.



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.



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

def filter_matcher(matcher)
  matcher
end