Class: CleanArchitecture::UseCases::Parameters

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/clean_architecture/use_cases/parameters.rb

Constant Summary collapse

INVALID_PARAMS_FAILURE_TYPE =
'expectation_failed'

Instance Method Summary collapse

Constructor Details

#initialize(context, dry_validation_result) ⇒ Parameters

Returns a new instance of Parameters.



13
14
15
16
# File 'lib/clean_architecture/use_cases/parameters.rb', line 13

def initialize(context, dry_validation_result)
  @context = context
  @dry_validation_result = dry_validation_result
end

Instance Method Details

#context(key) ⇒ Object



18
19
20
# File 'lib/clean_architecture/use_cases/parameters.rb', line 18

def context(key)
  @context.fetch(key)
end

#errorsObject



31
32
33
34
35
36
37
38
39
# File 'lib/clean_architecture/use_cases/parameters.rb', line 31

def errors
  new_errors = Errors.new(nil, INVALID_PARAMS_FAILURE_TYPE)
  @dry_validation_result.errors.to_h.each_pair do |invalid_attribute_key, error_messages|
    error_messages.each do |error_message|
      new_errors.add(invalid_attribute_key, error_message)
    end
  end
  new_errors
end

#to_monadObject



25
26
27
# File 'lib/clean_architecture/use_cases/parameters.rb', line 25

def to_monad
  @dry_validation_result.success? ? Success(@dry_validation_result.to_h) : Failure(errors)
end