Sha256: 8db94db27147aa95ccde5924b2df18a9c1bcfe1cf1960eb519718c932753920e

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module Protocol
  class ::Object
    # Returns true if this object conforms to +protocol+, otherwise false.
    #
    # This is especially useful, if check_failure in the protocol is set to
    # :none or :warning, and conformance of a class to a protocol should be
    # checked later in runtime.
    def conform_to?(protocol)
      protocol.check(self)
    end

    def conform_to(protocol)
      extend(protocol)
    end

    # Define a protocol configured by +block+. Look at the methods of
    # ProtocolModule to get an idea on how to do that.
    def Protocol(modul = nil, &block)
      pm = ProtocolModule.new(&block)
      if modul
        pm.infer(modul)
      else
        pm
      end
    end

    alias protocol Protocol
  end

  class ::Class
    # This method should be called at the end of a class definition, that is,
    # after all methods have been added to the class. The conformance to the
    # protocol of the class given as the argument is checked. See
    # Protocol::CHECK_MODES for the consequences of failure of this check.
    alias conform_to include

    # Returns true if this class conforms to +protocol+, otherwise false.
    #
    # This is especially useful, if check_failure in the protocol is set to
    # :none or :warning, and conformance of a class to a protocol should be
    # checked later in runtime.
    def conform_to?(protocol)
      protocol.check(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
protocol-2.0.1 lib/protocol/xt.rb
protocol-2.0.0 lib/protocol/xt.rb