Sha256: 8599b52b729ad8174c5a570dda0422a24204fe79632b6fbb59c655f7b320d7d7

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Protocol
  # This class is a proxy that stores postcondition blocks, which are called
  # after the result of the wrapped method was determined.
  class Postcondition
    instance_methods.each do |m|
      m.to_s =~ /\A(__|object_id|instance_eval\z|inspect\z)/ or undef_method m
    end

    def initialize(object)
      @object = object
      @blocks = []
    end

    # This is the alternative result "keyword".
    def __result__
      @result
    end

    # This is the result "keyword" which can be used to query the result of
    # wrapped method in a postcondition clause.
    def result
      if @object.respond_to? :result
        warn "#{@object.class} already defines a result method, "\
          "try __result__ instead"
        @object.__send__(:result)
      else
        @result
      end
    end

    # This is the "keyword" to be used instead of +self+ to refer to current
    # object.
    def myself
      @object
    end

    # :stopdoc:
    def __result__=(result)
      @result = result
    end

    def __check__
      @blocks.all? { |block| instance_eval(&block) }
    end

    def __add__(block)
      @blocks << block
      self
    end
    # :startdoc:

    # Send all remaining messages to the object.
    def method_missing(*a, &b)
      @object.__send__(*a, &b)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protocol-2.0.1 lib/protocol/post_condition.rb
protocol-2.0.0 lib/protocol/post_condition.rb
protocol-1.0.1 lib/protocol/post_condition.rb
protocol-1.0.0 lib/protocol/post_condition.rb