Class: Gaskit::ContractRegistry
- Inherits:
-
Object
- Object
- Gaskit::ContractRegistry
- 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
-
.verify_result_class!(result_class) ⇒ Object
Verifies that the given class is a subclass of
Gaskit::BaseResult.
Instance Method Summary collapse
-
#fetch(name) ⇒ Class
Fetches a registered contract's result class.
-
#initialize ⇒ ContractRegistry
constructor
Initializes a new instance of
ContractRegistry. -
#register(name, result_class, override: false) ⇒ void
Registers a contract with an associated result class.
-
#registered ⇒ Hash<Symbol, Class>
Returns a duplicate of all registered contracts.
-
#registered?(name) ⇒ Boolean
Checks if a contract is registered.
Constructor Details
#initialize ⇒ ContractRegistry
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
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
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
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 |
#registered ⇒ Hash<Symbol, Class>
Returns a duplicate of all registered contracts
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
51 52 53 |
# File 'lib/gaskit/contract_registry.rb', line 51 def registered?(name) @contracts.key?(name.to_sym) end |