Class: MuchRails::ChangeActionResult

Inherits:
Object
  • Object
show all
Defined in:
lib/much-rails/change_action_result.rb

Overview

MuchRails::ChangeActionResult is a Result object intended to wrap and compose a MuchRails::Result.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(save_service_result) ⇒ ChangeActionResult

Returns a new instance of ChangeActionResult.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/much-rails/change_action_result.rb', line 20

def initialize(save_service_result)
  unless save_service_result.is_a?(MuchRails::Result)
    raise(
      TypeError,
      "MuchRails::Result expected, got #{save_service_result.class}",
    )
  end

  @service_result = save_service_result

  @service_result.validation_errors ||= HashWithIndifferentAccess.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



55
56
57
# File 'lib/much-rails/change_action_result.rb', line 55

def method_missing(name, *args, &block)
  service_result&.__send__(name, *args, &block)
end

Instance Attribute Details

#service_resultObject (readonly)

Returns the value of attribute service_result.



18
19
20
# File 'lib/much-rails/change_action_result.rb', line 18

def service_result
  @service_result
end

Class Method Details

.failure(**kargs) ⇒ Object



14
15
16
# File 'lib/much-rails/change_action_result.rb', line 14

def self.failure(**kargs)
  new(MuchRails::Result.failure(**kargs))
end

.success(**kargs) ⇒ Object



10
11
12
# File 'lib/much-rails/change_action_result.rb', line 10

def self.success(**kargs)
  new(MuchRails::Result.success(**kargs))
end

Instance Method Details

#any_unextracted_validation_errors?Boolean

Returns:



49
50
51
# File 'lib/much-rails/change_action_result.rb', line 49

def any_unextracted_validation_errors?
  !!(failure? && validation_errors.any?)
end

#extract_validation_error(field_name) ⇒ Object



45
46
47
# File 'lib/much-rails/change_action_result.rb', line 45

def extract_validation_error(field_name)
  Array.wrap(validation_errors.delete(field_name)).compact
end

#validation_error_messagesObject



41
42
43
# File 'lib/much-rails/change_action_result.rb', line 41

def validation_error_messages
  validation_errors.values.flatten.compact
end

#validation_errorsObject



33
34
35
36
37
38
39
# File 'lib/much-rails/change_action_result.rb', line 33

def validation_errors
  @validation_errors ||=
    service_result
      .get_for_all_results(:validation_errors)
      .to_h
      .with_indifferent_access
end