Class: Gaskit::ContractRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/gaskit/contract_registry.rb

Overview

Represents a registry for managing contracts and their result classes

This class allows registering contracts with associated result classes, checking if they are registered, and fetching them for use. It also includes validation to ensure result classes adhere to the expected base class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContractRegistry

Initializes a new instance of ContractRegistry

Sets up the internal hash for storing contracts.



25
26
27
# File 'lib/gaskit/contract_registry.rb', line 25

def initialize
  @contracts = {}
end

Class Method Details

.verify_result_class!(result_class) ⇒ Object

Verifies that the given class is a subclass of Gaskit::BaseResult

Parameters:

  • result_class (Class)

    The class to verify

Raises:



17
18
19
# File 'lib/gaskit/contract_registry.rb', line 17

def verify_result_class!(result_class)
  raise Gaskit::ResultTypeError, result_class unless result_class <= Gaskit::OperationResult
end

Instance Method Details

#fetch(name) ⇒ Class

Fetches a registered contract's result class

Parameters:

  • name (Symbol, String)

    The name of the contract

Returns:

  • (Class)

    The result class for the contract

Raises:



60
61
62
63
64
65
# File 'lib/gaskit/contract_registry.rb', line 60

def fetch(name)
  @contracts.fetch(name.to_sym) do
    raise Gaskit::ContractError, "Contract #{name} not registered, register it with " \
                                 "Gaskit.configuration.contracts.register(name, result_class)"
  end
end

#register(name, result_class, override: false) ⇒ void

This method returns an undefined value.

Registers a contract with an associated result class

Parameters:

  • name (Symbol, String)

    The name of the contract

  • result_class (Class)

    The class that represents the result for the contract

  • override (Boolean) (defaults to: false)

    Whether to override an existing registration (default: false)

Raises:



37
38
39
40
41
42
43
44
45
# File 'lib/gaskit/contract_registry.rb', line 37

def register(name, result_class, override: false)
  name = name.to_sym
  raise Gaskit::ContractError, "Contract #{name} already registered" if @contracts.key?(name) && !override

  ContractRegistry.verify_result_class!(result_class)
  @contracts[name] = result_class.freeze

  Gaskit.configuration.logger.debug { "[Gaskit] Registered contract #{name}" } if Gaskit.debug?
end

#registeredHash<Symbol, Class>

Returns a duplicate of all registered contracts

Returns:

  • (Hash<Symbol, Class>)

    A hash of all registered contracts where keys are contract names and values are their corresponding result classes



71
72
73
# File 'lib/gaskit/contract_registry.rb', line 71

def registered
  @contracts.dup
end

#registered?(name) ⇒ Boolean

Checks if a contract is registered

Parameters:

  • name (Symbol, String)

    The name of the contract

Returns:

  • (Boolean)

    true if the contract is registered, otherwise false



51
52
53
# File 'lib/gaskit/contract_registry.rb', line 51

def registered?(name)
  @contracts.key?(name.to_sym)
end