Class: UseCases::Result

Inherits:
Dry::Monads::Result
  • Object
show all
Defined in:
lib/use_cases/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step, result) ⇒ Result

Returns a new instance of Result.



7
8
9
10
11
# File 'lib/use_cases/result.rb', line 7

def initialize(step, result)
  super()
  @step = step
  @result = result
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



13
14
15
16
17
# File 'lib/use_cases/result.rb', line 13

def method_missing(method, *args, &block)
  return result.send(method, *args, &block) if result.respond_to?(method)

  super
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/use_cases/result.rb', line 5

def result
  @result
end

#stepObject (readonly)

Returns the value of attribute step.



5
6
7
# File 'lib/use_cases/result.rb', line 5

def step
  @step
end

Instance Method Details

#failureObject



43
44
45
# File 'lib/use_cases/result.rb', line 43

def failure
  failure? && value
end

#failure?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/use_cases/result.rb', line 35

def failure?
  result.is_a?(Dry::Monads::Result::Failure)
end

#inspectObject



63
64
65
66
67
68
69
# File 'lib/use_cases/result.rb', line 63

def inspect
  if failure?
    "UseCases::Result.Failure(#{value.inspect})"
  else
    "UseCases::Result.Success(#{value.inspect})"
  end
end

#nil?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/use_cases/result.rb', line 47

def nil?
  value.nil?
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/use_cases/result.rb', line 19

def respond_to_missing?(method, include_private = false)
  result.respond_to?(method, include_private) || super
end

#result_empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/use_cases/result.rb', line 55

def result_empty?
  result.success? && result.value! == Dry::Monads::Unit
end

#result_not_monad?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/use_cases/result.rb', line 59

def result_not_monad?
  !result.is_a?(Dry::Monads::Result)
end

#successObject



39
40
41
# File 'lib/use_cases/result.rb', line 39

def success
  success? && value
end

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/use_cases/result.rb', line 31

def success?
  !failure?
end

#to_resultObject



51
52
53
# File 'lib/use_cases/result.rb', line 51

def to_result
  result
end

#valueObject Also known as: value!



23
24
25
26
27
28
# File 'lib/use_cases/result.rb', line 23

def value
  return result if result_not_monad?
  return if result_empty?

  result.success? ? result.value! : result.failure
end