SuppressionSupport.successful()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
1
# frozen_string_literal: true
2
3
require_relative 'errors'
4
5
module AMA
6
  module Entity
7
    class Mapper
8
      module Mixin
9
        # Special module with method for playing with error suppression
10
        module SuppressionSupport
11
          include Errors
12
13
          # Enumerates elements in enumerator, applying block to each one,
14
          # returning result or suppressing specified error. If no element
15
          # has succeeded, raises last error.
16
          #
17
          # @param [Enumerator] enumerator
18
          # @param [Class<? extends StandardError>] error
19
          # @param [AMA::Entity::Mapper::Context] ctx
20
          def successful(enumerator, error = StandardError, ctx = nil)
21
            suppressed = []
22
            enumerator.each do |*args|
23
              begin
24
                return yield(*args)
25
              rescue error => e
26
                ctx.logger.debug("#{e.class} raised: #{e.message}") if ctx
27
                suppressed.push(e)
28
              end
29
            end
30
            compliance_error('Empty enumerator passed') if suppressed.empty?
31
            raise suppressed.last
32
          end
33
        end
34
      end
35
    end
36
  end
37
end
38