Module: ConvenientService::Service::Plugins::CanHaveFallbacks::Concern

Includes:
ConvenientService::Support::Concern
Defined in:
lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fallback_error_resultObject

Returns ConvenientService::Service::Plugins::HasJSendResult::Entities::Result when #fallback_result is overridden.

Examples:

fallback_error_result method MUST always return success with reasonable "null" data.


For example, check the following service:

  class SelectActiveUsers
    include ConvenientService::Standard::Config

    # ...

    def result
      # ...

      success(users: ::User.where(active: true))
    end

    # ...
  end

Since `result` returns relation for `users` when it is successful, its corresponding `fallback_error_result` must also return a relation.

  class SelectActiveUsers
    include ConvenientService::Standard::Config

    # ...

    def fallback_error_result
      # ...

      success(users: ::User.none)
    end

    # ...
  end

This way a significant amount of extra `if` statements can be avoided.

result = SelectActiveUsers.result # or `result = SelectActiveUsers.fallback_error_result`

if result.success?
  result.data[:users].count
end

It is safe to invoke `count` for both `result` and `fallback_error_result` since `result.data[:users]` return same class.

This idea can be applied in a broader sense by utilizing the Null object pattern.

Raises:

See Also:



106
107
108
# File 'lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb', line 106

def fallback_error_result(...)
  new(...).fallback_error_result
end

.fallback_failure_resultObject

Returns ConvenientService::Service::Plugins::HasJSendResult::Entities::Result when #fallback_result is overridden.



45
46
47
# File 'lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb', line 45

def fallback_failure_result(...)
  new(...).fallback_failure_result
end

.fallback_resultObject

Returns ConvenientService::Service::Plugins::HasJSendResult::Entities::Result when #fallback_result is overridden.



115
116
117
# File 'lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb', line 115

def fallback_result(...)
  new(...).fallback_result
end

Instance Method Details

#fallback_error_resultObject

Returns ConvenientService::Service::Plugins::HasJSendResult::Entities::Result when overridden.



25
26
27
# File 'lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb', line 25

def fallback_error_result
  ::ConvenientService.raise Exceptions::FallbackResultIsNotOverridden.new(service: self, status: :error)
end

#fallback_failure_resultObject

Returns ConvenientService::Service::Plugins::HasJSendResult::Entities::Result when overridden.



16
17
18
# File 'lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb', line 16

def fallback_failure_result
  ::ConvenientService.raise Exceptions::FallbackResultIsNotOverridden.new(service: self, status: :failure)
end

#fallback_resultObject

Returns ConvenientService::Service::Plugins::HasJSendResult::Entities::Result when overridden.



34
35
36
# File 'lib/convenient_service/service/plugins/can_have_fallbacks/concern.rb', line 34

def fallback_result
  ::ConvenientService.raise Exceptions::FallbackResultIsNotOverridden.new(service: self, status: nil)
end