Sha256: 914f514e143e70320be002fbcb3a9cf684616cfba69d71e85fc0dca559df4b0c

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

class InvariantError < StandardError
  def to_contract_error
    self
  end
end

module Contracts
  module Invariants
    def self.included(base)
      common base
    end

    def self.extended(base)
      common base
    end

    def self.common(base)
      return if base.respond_to?(:Invariant)

      base.extend(InvariantExtension)
    end

    def verify_invariants!(method)
      return unless self.class.respond_to?(:invariants)

      self.class.invariants.each do |invariant|
        invariant.check_on(self, method)
      end
    end

    module InvariantExtension
      def Invariant(name, &condition)
        return if ENV["NO_CONTRACTS"]

        invariants << Invariant.new(self, name, &condition)
      end

      def invariants
        @invariants ||= []
      end
    end

    class Invariant
      def initialize(klass, name, &condition)
        @klass, @name, @condition = klass, name, condition
      end

      def expected
        "#{@name} condition to be true"
      end

      def check_on(target, method)
        return if target.instance_eval(&@condition)

        self.class.failure_callback(:expected => expected,
                                    :actual => false,
                                    :target => target,
                                    :method => method)
      end

      def self.failure_callback(data)
        raise InvariantError, failure_msg(data)
      end

      def self.failure_msg(data)
%{Invariant violation:
    Expected: #{data[:expected]}
    Actual: #{data[:actual]}
    Value guarded in: #{data[:target].class}::#{Support.method_name(data[:method])}
    At: #{Support.method_position(data[:method])}}
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contracts-0.5 lib/contracts/invariants.rb