Class: GraphMatching::Assertion

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_matching/assertion.rb

Overview

Provides expressive methods for common runtime assertions, e.g.

assert(banana).is_a(Fruit)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Assertion

Returns a new instance of Assertion.



11
12
13
# File 'lib/graph_matching/assertion.rb', line 11

def initialize(obj)
  @obj = obj
end

Instance Attribute Details

#objObject (readonly)

Returns the value of attribute obj.



9
10
11
# File 'lib/graph_matching/assertion.rb', line 9

def obj
  @obj
end

Instance Method Details

#eq(other) ⇒ Object



15
16
17
18
19
# File 'lib/graph_matching/assertion.rb', line 15

def eq(other)
  unless obj == other
    raise "Expected #{other}, got #{obj}"
  end
end

#gte(other) ⇒ Object



21
22
23
24
25
# File 'lib/graph_matching/assertion.rb', line 21

def gte(other)
  unless obj >= other
    raise "Expected #{obj} to be >= #{other}"
  end
end

#is_a(klass) ⇒ Object

rubocop:disable Naming/PredicateName



28
29
30
31
32
# File 'lib/graph_matching/assertion.rb', line 28

def is_a(klass)
  unless obj.is_a?(klass)
    raise TypeError, "Expected #{klass}, got #{obj.class}"
  end
end

#not_nilObject

rubocop:enable Naming/PredicateName



35
36
37
38
39
# File 'lib/graph_matching/assertion.rb', line 35

def not_nil
  if obj.nil?
    raise 'Unexpected nil'
  end
end