class Pacto::ContractRegistry

Public Class Methods

new() click to toggle source
# File lib/pacto/core/contract_registry.rb, line 3
def initialize
  @registry = Hash.new { |hash, key| hash[key] = Set.new }
end

Public Instance Methods

[](tag) click to toggle source
# File lib/pacto/core/contract_registry.rb, line 7
def [](tag)
  @registry[tag]
end
contracts_for(request_signature) click to toggle source
# File lib/pacto/core/contract_registry.rb, line 33
def contracts_for(request_signature)
  all_contracts.select { |c| c.matches? request_signature }
end
register(contract, *tags) click to toggle source
# File lib/pacto/core/contract_registry.rb, line 11
def register(contract, *tags)
  tags << :default if tags.empty?

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

  self
end
use(tag, values = {}) click to toggle source
# File lib/pacto/core/contract_registry.rb, line 21
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

Private Instance Methods

all_contracts() click to toggle source
# File lib/pacto/core/contract_registry.rb, line 39
def all_contracts
  @registry.values.inject(Set.new, :+)
end