Class: BaconExpect::Matcher::BeWithin

Inherits:
Object
  • Object
show all
Defined in:
lib/bacon-expect/matchers/be_within.rb

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ BeWithin

Returns a new instance of BeWithin.



3
4
5
# File 'lib/bacon-expect/matchers/be_within.rb', line 3

def initialize(range)
  @range = range
end

Instance Method Details

#fail!(subject, negated) ⇒ Object

Raises:



17
18
19
# File 'lib/bacon-expect/matchers/be_within.rb', line 17

def fail!(subject, negated)
  raise FailedExpectation.new(FailMessageRenderer.message_for_be_within(negated, subject, @range, @center_value))
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



12
13
14
15
# File 'lib/bacon-expect/matchers/be_within.rb', line 12

def matches?(subject)
  raise InvalidMatcher.new("be_within matcher incomplete. Missing .of value") unless @center_value
  (subject - @center_value).abs <= @range
end

#of(center_value) ⇒ Object



7
8
9
10
# File 'lib/bacon-expect/matchers/be_within.rb', line 7

def of(center_value)
  @center_value = center_value
  self
end