Class: Contracts::Expects

Inherits:
Base
  • Object
show all
Defined in:
lib/contracts.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#method

Instance Method Summary collapse

Methods inherited from Base

#+@, #method_name

Constructor Details

#initialize(expectations) ⇒ Expects

Returns a new instance of Expects.



143
144
145
# File 'lib/contracts.rb', line 143

def initialize(expectations)
  @expectations = expectations
end

Instance Attribute Details

#expectationsObject (readonly)

Returns the value of attribute expectations.



141
142
143
# File 'lib/contracts.rb', line 141

def expectations
  @expectations
end

Instance Method Details

#before_call(receiver, *args, &blk) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/contracts.rb', line 147

def before_call(receiver, *args, &blk)
  @parameter_names ||= method.parameters.map(&:last)

  @parameter_names.each_with_index do |parameter_name, idx|
    next unless expectation = expectations[parameter_name]

    Expectation.match! args[idx], expectation
  end
rescue Expectation::Error
  raise Contracts::Error, "#{$!} in call to `#{method_name}`", caller[5..-1]
end