Class: Handshake::ProcContract
- Inherits:
-
Object
- Object
- Handshake::ProcContract
- 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
Instance Attribute Summary collapse
-
#accepts ⇒ Object
:nodoc:.
-
#returns ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #accepts_varargs? ⇒ Boolean
- #check_accepts!(*args, &block) ⇒ Object
- #check_returns!(*args) ⇒ Object
-
#initialize ⇒ ProcContract
constructor
A new instance of ProcContract.
-
#signature=(contract_hash) ⇒ Object
Accepts signatures of the form: Clause => Clause [ Clause, Clause ] => Clause.
Constructor Details
#initialize ⇒ ProcContract
Returns a new instance of ProcContract.
336 337 338 |
# File 'lib/handshake.rb', line 336 def initialize @accepts, @returns = [], [] end |
Instance Attribute Details
#accepts ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/handshake.rb', line 334 def accepts @accepts end |
#returns ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/handshake.rb', line 334 def returns @returns end |
Instance Method Details
#accepts_varargs? ⇒ Boolean
366 367 368 |
# File 'lib/handshake.rb', line 366 def accepts_varargs? accepts.last.is_a? Array end |
#check_accepts!(*args, &block) ⇒ Object
350 351 352 353 354 355 356 357 358 |
# File 'lib/handshake.rb', line 350 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
360 361 362 363 364 |
# File 'lib/handshake.rb', line 360 def check_returns!(*args) @returns.each_with_index do |expected, i| check_equivalence!(args[i], expected) end end |
#signature=(contract_hash) ⇒ Object
343 344 345 346 347 348 |
# File 'lib/handshake.rb', line 343 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 |