require 'rubygems' require 'decorator' require 'translate' class ContractDecorator < Decorator class << self attr_accessor :contract_class end def initialize(object) super(object) @contract = self.__class__.contract_class.new 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 def ContractDecorator(contract_class) klass = Class.new(ContractDecorator) klass.contract_class = contract_class klass end