Class: Contracts::Invariants::Invariant

Inherits:
Object
  • Object
show all
Defined in:
lib/contracts-ruby2/lib/contracts/invariants.rb,
lib/contracts-ruby3/lib/contracts/invariants.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, &condition) ⇒ Invariant

Returns a new instance of Invariant.



38
39
40
# File 'lib/contracts-ruby2/lib/contracts/invariants.rb', line 38

def initialize(klass, name, &condition)
  @klass, @name, @condition = klass, name, condition
end

Class Method Details

.failure_callback(data) ⇒ Object



55
56
57
# File 'lib/contracts-ruby2/lib/contracts/invariants.rb', line 55

def self.failure_callback(data)
  fail InvariantError, failure_msg(data)
end

.failure_msg(data) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/contracts-ruby2/lib/contracts/invariants.rb', line 59

def self.failure_msg(data)
  %{Invariant violation:
      Expected: #{data[:expected]}
      Actual: #{data[:actual]}
      Value guarded in: #{data[:target].class}::#{Support.method_name(data[:method])}
      At: #{Support.method_position(data[:method])}}
end

Instance Method Details

#check_on(target, method) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/contracts-ruby2/lib/contracts/invariants.rb', line 46

def check_on(target, method)
  return if target.instance_eval(&@condition)

  self.class.failure_callback(:expected => expected,
                              :actual => false,
                              :target => target,
                              :method => method)
end

#expectedObject



42
43
44
# File 'lib/contracts-ruby2/lib/contracts/invariants.rb', line 42

def expected
  "#{@name} condition to be true"
end