Sha256: b90f643dd8e3f6cca81c3f60671d04d323c50511ada08816db20fee048b28fa3

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Finitio
  class Contract
    include Metadata

    def initialize(infotype, dresser, undresser, name = nil, metadata = nil)
      unless infotype.is_a?(Type)
        raise ArgumentError, "Type expected, got `#{infotype}`"
      end
      unless dresser.respond_to?(:call)
        raise ArgumentError, "r(:call) expected, got `#{dresser}`"
      end
      unless undresser.respond_to?(:call)
        raise ArgumentError, "r(:call) expected, got `#{undresser}`"
      end
      unless name.nil? or name.is_a?(Symbol)
        raise ArgumentError, "Symbol expected, got `#{name}`"
      end

      @name      = name
      @infotype  = infotype
      @dresser   = dresser
      @undresser = undresser
      @metadata  = metadata
    end
    attr_reader :name, :infotype, :dresser, :undresser

    def bind_ruby_type(clazz)
      @dresser   = clazz.method(name.to_sym)
      @undresser = clazz.instance_method(:"to_#{name}")
    end

    def hash
      infotype.hash ^ dresser.hash ^ undresser.hash
    end

    def ==(other)
      super || (
        other.is_a?(Contract) &&
        name == other.name &&
        infotype == other.infotype &&
        dresser == other.dresser &&
        undresser == other.undresser
      )
    end
    alias :eql? :==

  end # class Contract
end # module Finitio

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
finitio-0.7.0 lib/finitio/support/contract.rb