Class: Servactory::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/servactory/result.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, exception: nil) ⇒ Result

Returns a new instance of Result.



41
42
43
44
# File 'lib/servactory/result.rb', line 41

def initialize(context:, exception: nil)
  @context = context
  @exception = exception
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



69
70
71
72
73
# File 'lib/servactory/result.rb', line 69

def method_missing(name, *args, &block)
  super
rescue NoMethodError => e
  rescue_no_method_error_with(exception: e)
end

Class Method Details

.failure_for(context:, exception:) ⇒ Object



37
38
39
# File 'lib/servactory/result.rb', line 37

def self.failure_for(context:, exception:)
  new(context:, exception:).send(:as_failure)
end

.success_for(context:) ⇒ Object



33
34
35
# File 'lib/servactory/result.rb', line 33

def self.success_for(context:)
  new(context:).send(:as_success)
end

Instance Method Details

#inspectObject



53
54
55
# File 'lib/servactory/result.rb', line 53

def inspect
  "#<#{self.class.name} #{draw_result}>"
end

#on_failure(type = :all) {|outputs:, exception: @exception| ... } ⇒ Object

Yields:

  • (outputs:, exception: @exception)


63
64
65
66
67
# File 'lib/servactory/result.rb', line 63

def on_failure(type = :all)
  yield(outputs:, exception: @exception) if failure? && [:all, @exception&.type].include?(type)

  self
end

#on_success {|outputs:| ... } ⇒ Object

Yields:

  • (outputs:)


57
58
59
60
61
# File 'lib/servactory/result.rb', line 57

def on_success
  yield(outputs:) if success?

  self
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/servactory/result.rb', line 75

def respond_to_missing?(*)
  super
end

#to_hObject



46
47
48
49
50
51
# File 'lib/servactory/result.rb', line 46

def to_h
  methods(false)
    .reject { |key| key.in?(STATE_PREDICATE_NAMES) || key.to_s.end_with?("?") }
    .to_h { |key| [key, public_send(key)] }
    .compact
end