Class: Handshake::Proxy

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(proxied) ⇒ Proxy

Accepts an object to be proxied.



516
517
518
# File 'lib/handshake.rb', line 516

def initialize(proxied)
  @proxied = proxied
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

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



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/handshake.rb', line 567

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?
    block.contract = contract.block_contract
    return_val = @proxied.send(meth_name, *args) { |*argz| block.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_classObject

Returns the class of the proxied object.



527
528
529
# File 'lib/handshake.rb', line 527

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



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/handshake.rb', line 537

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?
    block.contract = contract.block_contract
    return_val = @proxied.send(meth_name, *args) { |*argz| block.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.



522
523
524
# File 'lib/handshake.rb', line 522

def unchecked!
  @proxied
end