Sha256: a0de46b469b8d0e5bd3a42620a2f4eda6c564054004c54495cf319ca02fb85a5

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'translate'

class Proxy

  def initialize(object, contract)
    @object = object
    @contract = contract
  end

  def method_missing(method, *args)
    # clone the object under contract for use in the pre condition
    object_pre = @object.clone

    # call pre condition
    method_pre = Translate.method_pre(method)
    if @contract.respond_to?(method_pre)
      @contract.send(method_pre, @object, *args)
    end

    # call the wrapped method
    exception = nil
    begin
      result = @object.send(method, *args)
    rescue => exception
    end

    # call the invariant condition
    if @contract.respond_to?(:invariant)
      @contract.invariant(@object)
    end

    # call the post condition
    method_post = Translate.method_post(method)
    if @contract.respond_to?(method_post)
      @contract.send(method_post, object_pre, @object, exception, result, *args)
    end

    # return the return value of the wrapped method call or raise the exception
    if exception.nil?
      result
    else
      raise exception
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
armin-joellenbeck-rdbc-0.0.4 lib/proxy.rb
armin-joellenbeck-rdbc-0.0.5 lib/proxy.rb