Sha256: 3f4f1cdacfe37b0245c9b77eca6ed3f4f28e9c3b94adf69ecf9038880a01a867

Contents?: true

Size: 840 Bytes

Versions: 2

Compression:

Stored size: 840 Bytes

Contents

module Pacto
  class ContractRegistry
    def initialize
      @registry = Hash.new { |hash, key| hash[key] = Set.new }
    end

    def [](tag)
      @registry[tag]
    end

    def register(contract, *tags)
      tags << :default if tags.empty?

      tags.each do |tag|
        @registry[tag] << contract
      end

      self
    end

    def use(tag, values = {})
      merged_contracts = @registry[:default] + @registry[tag]

      fail ArgumentError, "contract \"#{tag}\" not found" if merged_contracts.empty?

      merged_contracts.each do |contract|
        contract.stub_contract! values
      end

      self
    end

    def contracts_for(request_signature)
      all_contracts.select { |c| c.matches? request_signature }
    end

    private

    def all_contracts
      @registry.values.inject(Set.new, :+)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pacto-0.3.1 lib/pacto/core/contract_registry.rb
pacto-0.3.0 lib/pacto/core/contract_registry.rb