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