Class: Operations::Result

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

Overview

This is a the result of the operation. Considered a failure if contains any errors. Contains all the execution artifacts such as params and context (the initial one merged with the result of contract and operation routine execution). Also able to spawn a form object basing on the operation params and errors.

Instance Method Summary collapse

Instance Method Details

#as_json(include_command: false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/operations/result.rb', line 95

def as_json(*, include_command: false, **)
  hash = {
    component: component,
    params: params,
    context: context_as_json,
    on_success: on_success.as_json,
    on_failure: on_failure.as_json,
    errors: errors(full: true).to_h
  }
  hash[:command] = operation.as_json if include_command
  hash
end

#errors(**options) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/operations/result.rb', line 27

def errors(**options)
  if @errors.respond_to?(:call)
    @errors.call(**options)
  else
    options.empty? ? @errors : @errors.with([], options).freeze
  end
end

#failed_policy?(*error_codes) ⇒ Boolean Also known as: failed_policies?

Checks if ANY of the passed policy codes have failed If nothing is passed - checks that ANY policy have failed

Returns:

  • (Boolean)


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

def failed_policy?(*error_codes)
  component == :policies && failed_precheck?(*error_codes)
end

#failed_precheck?(*error_codes) ⇒ Boolean Also known as: failed_prechecks?

Checks if ANY of the passed precondition or policy codes have failed If nothing is passed - checks that ANY precondition or policy have failed

Returns:

  • (Boolean)


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

def failed_precheck?(*error_codes)
  failure? &&
    %i[policies preconditions].include?(component) &&
    (error_codes.blank? || errors_with_code?(*error_codes))
end

#failed_precondition?(*error_codes) ⇒ Boolean Also known as: failed_preconditions?

Checks if ANY of the passed precondition codes have failed If nothing is passed - checks that ANY precondition have failed

Returns:

  • (Boolean)


62
63
64
# File 'lib/operations/result.rb', line 62

def failed_precondition?(*error_codes)
  component == :preconditions && failed_precheck?(*error_codes)
end

#failure?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/operations/result.rb', line 40

def failure?
  !success?
end

#formObject

A form object that can be used for rendering forms with ‘form_for`, `simple_form` and other view helpers.



73
74
75
76
77
78
# File 'lib/operations/result.rb', line 73

def form
  @form ||= operation.form_class.new(
    operation.form_hydrator.call(operation.form_class, params, **context),
    messages: errors.to_h
  )
end

#merge(**changes) ⇒ Object

Instantiates a new result with the given fields updated



23
24
25
# File 'lib/operations/result.rb', line 23

def merge(**changes)
  self.class.new(**self.class.dry_initializer.attributes(self), **changes)
end

#pretty_print(pp) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/operations/result.rb', line 80

def pretty_print(pp)
  attributes = self.class.dry_initializer.attributes(self)

  pp.object_group(self) do
    pp.seplist(attributes.keys, -> { pp.text "," }) do |name|
      pp.breakable " "
      pp.group(1) do
        pp.text name.to_s
        pp.text " = "
        pp.pp send(name)
      end
    end
  end
end

#success?Boolean Also known as: callable?

Returns:

  • (Boolean)


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

def success?
  errors.empty?
end

#to_monadObject



67
68
69
# File 'lib/operations/result.rb', line 67

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