Class: OmniService::Result

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/omni_service/result.rb

Overview

Structured result of component execution.

Attributes:

  • operation: the component that produced this result

  • shortcut: component that triggered early exit (if any)

  • params: array of transformed params

  • context: accumulated key-value pairs from all components

  • errors: array of OmniService::Error

  • on_success: results from transaction success callbacks

  • on_failure: results from transaction failure callbacks

Components return Success/Failure monads which are converted to Result:

  • Success(key: value) => Result(context: { key: value })

  • Success(params, key: value) => Result(params: [params], context: { key: value })

  • Failure(:code) => Result(errors: [Error(code: :code)])

  • Failure([{ code:, path:, message: }]) => Result(errors: […])

Examples:

Checking result

result = operation.call(params, **context)
result.success?  # => true/false
result.failure?  # => true/false
result.context   # => { post: <Post>, author: <Author> }
result.errors    # => [#<Error code=:blank path=[:title]>]

Converting to monad

result.to_monad  # => Success(result) or Failure(result)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(operation) ⇒ Object



61
62
63
# File 'lib/omni_service/result.rb', line 61

def self.build(operation, **)
  new(operation:, params: [], context: {}, errors: [], **)
end

.process(component, result) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/omni_service/result.rb', line 46

def self.process(component, result)
  case result
  in OmniService::Result
    result
  in Success(params, *other_params, Hash => context) if context.keys.all?(Symbol)
    OmniService::Result.build(component, params: [params, *other_params], context:)
  in Success(Hash => context) if context.keys.all?(Symbol)
    OmniService::Result.build(component, context:)
  in Failure[*failures]
    OmniService::Result.build(component, errors: OmniService::Error.process(component, failures))
  else
    raise "Invalid callable result `#{result.inspect}` returned by `#{component.inspect}`"
  end
end

Instance Method Details

#deconstruct_keys(_) ⇒ Object



92
93
94
# File 'lib/omni_service/result.rb', line 92

def deconstruct_keys(_)
  { operation:, params:, context:, errors: }
end

#failure?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/omni_service/result.rb', line 84

def failure?
  !success?
end

#merge(other = nil, **changes) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/omni_service/result.rb', line 65

def merge(other = nil, **changes)
  if other
    self.class.new(
      operation:,
      shortcut: shortcut || other.shortcut,
      **merged_attributes(other),
      **changes
    )
  else
    self.class.new(**self.class.dry_initializer.attributes(self), **changes)
  end
end

#success?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/omni_service/result.rb', line 80

def success?
  errors.empty?
end

#to_monadObject



88
89
90
# File 'lib/omni_service/result.rb', line 88

def to_monad
  success? ? Success(self) : Failure(self)
end