Class: Handshake::Proxy
- Inherits:
-
Object
- Object
- Handshake::Proxy
- Defined in:
- lib/handshake.rb
Overview
This class filters all method calls to its proxied object through any contracts defined on that object’s class. It attempts to look and act like its proxied object for all intents and purposes, although it notably does not proxy __id__
, __send__
, or class
.
Constant Summary collapse
- NOT_PROXIED =
[ "__id__", "__send__" ]
- SELF_PROXIED =
Object.instance_methods - NOT_PROXIED
Instance Method Summary collapse
-
#initialize(proxied) ⇒ Proxy
constructor
Accepts an object to be proxied.
-
#proxied_class ⇒ Object
Returns the class of the proxied object.
-
#send(meth_name, *args, &block) ⇒ Object
(also: #method_missing)
Override the send method, and alias method_missing to same.
-
#unchecked! ⇒ Object
Returns the wrapped object.
Constructor Details
#initialize(proxied) ⇒ Proxy
Accepts an object to be proxied.
498 499 500 501 |
# File 'lib/handshake.rb', line 498 def initialize(proxied) @proxied = proxied @proxied.instance_variable_set(:@checked_self, self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
Override the send method, and alias method_missing to same. This method intercepts all method calls and runs them through the contract filter. The order of contract checks is as follows:
-
Before: invariants, method signature, precondition
-
Method is called
-
After: method signature, postcondition, invariants
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/handshake.rb', line 550 def send(meth_name, *args, &block) meth_string = "#{@proxied.class}##{meth_name}" contract = @proxied.class.contract_for(meth_name) return_val = nil # Use throw/catch rather than raise/rescue in order to pull exceptions # once and only once from within the stack trace. Handshake.catch_contract("Contract violated in call to #{meth_string}") do @proxied.check_invariants! contract.check_accepts!(*args, &block) contract.check_pre! @proxied, *args end # make actual call, wrapping the given block in a new block so that # contract checks work if receiver uses yield. return_val = nil if contract.expects_block? cp = CheckedProc.new(contract.block_contract, &block) return_val = @proxied.send(meth_name, *args) { |*argz| cp.call(*argz) } else return_val = @proxied.send(meth_name, *args, &block) end Handshake.catch_contract("Contract violated by #{meth_string}") do contract.check_returns! return_val contract.check_post! @proxied, *(args << return_val) @proxied.check_invariants! end return return_val end |
Instance Method Details
#proxied_class ⇒ Object
Returns the class of the proxied object.
510 511 512 |
# File 'lib/handshake.rb', line 510 def proxied_class @proxied.class end |
#send(meth_name, *args, &block) ⇒ Object Also known as: method_missing
Override the send method, and alias method_missing to same. This method intercepts all method calls and runs them through the contract filter. The order of contract checks is as follows:
-
Before: invariants, method signature, precondition
-
Method is called
-
After: method signature, postcondition, invariants
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/handshake.rb', line 520 def send(meth_name, *args, &block) meth_string = "#{@proxied.class}##{meth_name}" contract = @proxied.class.contract_for(meth_name) return_val = nil # Use throw/catch rather than raise/rescue in order to pull exceptions # once and only once from within the stack trace. Handshake.catch_contract("Contract violated in call to #{meth_string}") do @proxied.check_invariants! contract.check_accepts!(*args, &block) contract.check_pre! @proxied, *args end # make actual call, wrapping the given block in a new block so that # contract checks work if receiver uses yield. return_val = nil if contract.expects_block? cp = CheckedProc.new(contract.block_contract, &block) return_val = @proxied.send(meth_name, *args) { |*argz| cp.call(*argz) } else return_val = @proxied.send(meth_name, *args, &block) end Handshake.catch_contract("Contract violated by #{meth_string}") do contract.check_returns! return_val contract.check_post! @proxied, *(args << return_val) @proxied.check_invariants! end return return_val end |
#unchecked! ⇒ Object
Returns the wrapped object. Method calls made against this object will not be checked.
505 506 507 |
# File 'lib/handshake.rb', line 505 def unchecked! @proxied end |