Class: OmniService::Params
- Inherits:
-
Object
- Object
- OmniService::Params
- Extended by:
- Dry::Initializer
- Defined in:
- lib/omni_service/params.rb
Overview
Validates params against a Dry::Validation::Contract.
Class Method Summary collapse
Instance Method Summary collapse
- #call(params = {}, **context) ⇒ Object
-
#initialize(contract = nil) ⇒ Params
constructor
A new instance of Params.
Constructor Details
#initialize(contract = nil) ⇒ Params
Returns a new instance of Params.
23 24 25 26 |
# File 'lib/omni_service/params.rb', line 23 def initialize(contract = nil, **, &) contract_class = Class.new(Dry::Validation::Contract, &) super(contract || contract_class.new, **) end |
Class Method Details
.params ⇒ Object
19 20 21 |
# File 'lib/omni_service/params.rb', line 19 def self.params(**, &) new(**) { params(&) } end |
.to_failure(message) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/omni_service/params.rb', line 10 def self.to_failure() { code: (.predicate if .respond_to?(:predicate)) || :invalid, message: .dump, path: .path, tokens: .respond_to?(:meta) ? . : {} } end |
Instance Method Details
#call(params = {}, **context) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/omni_service/params.rb', line 28 def call(params = {}, **context) contract_result = contract.call(params, context) if optional && contract_result.to_h.empty? OmniService::Result.build(self, params: [{}]) else to_operation_result(contract_result) end end |