Sha256: cf35253725b87c78358380b49e6619f863af8ca37e4786a3897a47c90e54a80d

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

# -*- encoding : utf-8 -*-
module Pacto
  module Cops
    extend Pacto::Resettable

    class << self
      def reset!
        @active_cops = nil
      end

      def register_cop(cop)
        fail TypeError "#{cop} does not respond to investigate" unless cop.respond_to? :investigate
        registered_cops << cop
      end

      def registered_cops
        @registered_cops ||= Set.new
      end

      def active_cops
        @active_cops ||= registered_cops.dup
      end

      def investigate(request_signature, pacto_response)
        return unless Pacto.validating?

        contract = Pacto.contracts_for(request_signature).first
        if contract
          investigation = perform_investigation request_signature, pacto_response, contract
        else
          investigation = Investigation.new request_signature, pacto_response
        end

        Pacto::InvestigationRegistry.instance.register_investigation investigation
      end

      def perform_investigation(request, response, contract)
        citations = []
        active_cops.map do | cop |
          citations.concat cop.investigate(request, response, contract)
        end
        Investigation.new(request, response, contract, citations.compact)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pacto-0.4.0.rc3 lib/pacto/cops.rb
pacto-0.4.0.rc2 lib/pacto/cops.rb
pacto-0.4.0.rc1 lib/pacto/cops.rb