Class: Kirei::Services::Result

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/kirei/services/result.rb

Constant Summary collapse

ErrorType =
type_member { { fixed: T::Array[Errors::JsonApiError] } }
ResultType =
type_member { { upper: Object } }

Instance Method Summary collapse

Constructor Details

#initialize(result: nil, errors: []) ⇒ Result

Returns a new instance of Result.



19
20
21
22
23
24
25
26
# File 'lib/kirei/services/result.rb', line 19

def initialize(result: nil, errors: [])
  if (result && !errors.empty?) || (!result && errors.empty?)
    raise ArgumentError, "Must provide either result or errors, got both or neither"
  end

  @result = result
  @errors = errors
end

Instance Method Details

#errorsObject



46
47
48
49
50
# File 'lib/kirei/services/result.rb', line 46

def errors
  raise "Cannot call 'errors' when there is a result" if success?

  @errors
end

#failed?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/kirei/services/result.rb', line 34

def failed?
  !success?
end

#resultObject



39
40
41
42
43
# File 'lib/kirei/services/result.rb', line 39

def result
  raise "Cannot call 'result' when there are errors" if failed?

  T.must(@result)
end

#success?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/kirei/services/result.rb', line 29

def success?
  @errors.empty?
end