Module: ServiceOperation::ErrorHandling

Defined in:
lib/service_operation/error_handling.rb

Overview

ActiveModel compatable ErrorHandling depends on Base#context and Errors

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



79
80
81
82
83
84
85
86
87
88
# File 'lib/service_operation/error_handling.rb', line 79

def method_missing(method_name, *args, &block)
  # {if/unless}, method_name
  conditional, object_method = (method_name.to_s.match(FAIL_IF_UNLESS_REGEXP) || [])[1..2]

  if conditional
    fail_conditional_object(conditional, object_method + '?', *args)
  else
    super
  end
end

Instance Method Details

#errorsObject



7
8
9
# File 'lib/service_operation/error_handling.rb', line 7

def errors
  @errors ||= Errors.new
end

#fail!(error = {}, more = {}) ⇒ Object

Parameters:



12
13
14
15
16
17
18
19
20
21
# File 'lib/service_operation/error_handling.rb', line 12

def fail!(error = {}, more = {})
  return unless error

  error = errors_from_error_code(error)
  errors.coerced_merge error

  more[:errors] ||= errors.merge(more[:errors] || {})

  context.fail! more
end

#fail_if_errors!Object

fail if there #errors Hash has any contents



24
25
26
# File 'lib/service_operation/error_handling.rb', line 24

def fail_if_errors!
  invalid? && fail!
end

#invalid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/service_operation/error_handling.rb', line 28

def invalid?
  errors.any?
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/service_operation/error_handling.rb', line 32

def valid?
  !invalid?
end