Class: Handshake::ProcContract

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

Overview

A ProcContract encapsulates knowledge about the signature of a method and can check arrays of values against the signature through the check_equivalence! method.

Direct Known Subclasses

MethodContract

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcContract

Returns a new instance of ProcContract.



317
318
319
# File 'lib/handshake.rb', line 317

def initialize
  @accepts, @returns = [], []
end

Instance Attribute Details

#acceptsObject

:nodoc:



315
316
317
# File 'lib/handshake.rb', line 315

def accepts
  @accepts
end

#returnsObject

:nodoc:



315
316
317
# File 'lib/handshake.rb', line 315

def returns
  @returns
end

Instance Method Details

#accepts_varargs?Boolean

Returns:

  • (Boolean)


347
348
349
# File 'lib/handshake.rb', line 347

def accepts_varargs?
  accepts.last.is_a? Array
end

#check_accepts!(*args, &block) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'lib/handshake.rb', line 331

def check_accepts!(*args, &block)
  @accepts.each_with_index do |expected_arg, i|
    # Varargs: consume all remaining arguments.
    if expected_arg.is_a? Array
      check_varargs!(args, expected_arg.first, i) and break
    end
    check_equivalence!(args[i], expected_arg)
  end
end

#check_returns!(*args) ⇒ Object



341
342
343
344
345
# File 'lib/handshake.rb', line 341

def check_returns!(*args)
  @returns.each_with_index do |expected, i|
    check_equivalence!(args[i], expected)
  end
end

#signature=(contract_hash) ⇒ Object

Accepts signatures of the form:

Clause => Clause
[ Clause, Clause ] => Clause

Raises:

  • (ArgumentError)


324
325
326
327
328
329
# File 'lib/handshake.rb', line 324

def signature=(contract_hash)
  raise ArgumentError unless contract_hash.length == 1
  sig_accepts, sig_returns = [ contract_hash.keys.first, contract_hash.values.first ].map {|v| arrayify v}
  self.accepts = sig_accepts
  self.returns = sig_returns
end