Class: Handshake::Proxy
- Inherits:
-
Object
- Object
- Handshake::Proxy
- Defined in:
- lib/handshake/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.
497 498 499 |
# File 'lib/handshake/handshake.rb', line 497 def initialize(proxied) @proxied = proxied 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
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/handshake/handshake.rb', line 541 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 return_val = @proxied.send meth_name, *args, &block 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.
508 509 510 |
# File 'lib/handshake/handshake.rb', line 508 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
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/handshake/handshake.rb', line 518 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 return_val = @proxied.send meth_name, *args, &block 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.
503 504 505 |
# File 'lib/handshake/handshake.rb', line 503 def unchecked! @proxied end |