Class: Rosette::Core::BufferedErrorReporter

Inherits:
ErrorReporter show all
Defined in:
lib/rosette/core/error_reporters/buffered_error_reporter.rb

Overview

Stores warnings errors in an internal buffer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ErrorReporter

#with_error_reporting

Constructor Details

#initializeBufferedErrorReporter

Returns a new instance of BufferedErrorReporter.


15
16
17
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 15

def initialize
  reset
end

Instance Attribute Details

#errorsArray<Exception> (readonly)

Returns the list of collected errors.

Returns:

  • (Array<Exception>)

    the list of collected errors.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 12

class BufferedErrorReporter < ErrorReporter
  attr_reader :errors, :warnings

  def initialize
    reset
  end

  # Add an error object to the list of collected errors.
  #
  # @param [Exception] error The error object to add.
  # @param [Hash] options A hash of options to record with the error.
  # @return [void]
  def report_error(error, options = {})
    errors << { error: error, options: options }
  end

  # Add an error object to the list of collected warnings.
  #
  # @param [Exception] error The error object to add.
  # @param [Hash] options A hash of options to record with the error.
  # @return [void]
  def report_warning(error, options = {})
    warnings << { error: error, options: options }
  end

  # Clears all errors and warnings
  #
  # @return [void]
  def reset
    @errors = []
    @warnings = []
  end

  # Returns true if one or more errors has been added, false otherwise.
  #
  # @return [Boolean] Whether or not one or more errors have been added.
  def errors_found?
    errors.size > 0
  end

  # Returns true if one or more warnings has been added, false otherwise.
  #
  # @return [Boolean] Whether or not one or more errors have been added.
  def warnings_found?
    warnings.size > 0
  end

  # Iterates over and yields each error. If no block is given, returns
  # an +Enumerator+.
  #
  # @yield [error, options] each consecutive error and options hash.
  # @yieldparam warning [Exception] the error
  # @yieldparam options [Hash] the hash of options associated with +error+
  # @return [nil, Enumerator] +nil+ if no block is given, an +Enumerator+
  #   otherwise.
  def each_error(&block)
    if block_given?
      errors.each do |error_hash|
        yield error_hash[:error], error_hash[:options]
      end
    else
      to_enum(__method__)
    end
  end

  # Iterates over and yields each warning. If no block is given, returns
  # an +Enumerator+.
  #
  # @yield [warning, options] each consecutive warning and options hash.
  # @yieldparam warning [Exception] the warning
  # @yieldparam options [Hash] the hash of options associated with +warning+
  # @return [nil, Enumerator] +nil+ if no block is given, an +Enumerator+
  def each_warning(&block)
    if block_given?
      warnings.each do |warning_hash|
        yield warning_hash[:error], warning_hash[:options]
      end
    else
      to_enum(__method__)
    end
  end
end

#warningsArray<Exception> (readonly)

Returns the list of collected warnings.

Returns:

  • (Array<Exception>)

    the list of collected warnings.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 12

class BufferedErrorReporter < ErrorReporter
  attr_reader :errors, :warnings

  def initialize
    reset
  end

  # Add an error object to the list of collected errors.
  #
  # @param [Exception] error The error object to add.
  # @param [Hash] options A hash of options to record with the error.
  # @return [void]
  def report_error(error, options = {})
    errors << { error: error, options: options }
  end

  # Add an error object to the list of collected warnings.
  #
  # @param [Exception] error The error object to add.
  # @param [Hash] options A hash of options to record with the error.
  # @return [void]
  def report_warning(error, options = {})
    warnings << { error: error, options: options }
  end

  # Clears all errors and warnings
  #
  # @return [void]
  def reset
    @errors = []
    @warnings = []
  end

  # Returns true if one or more errors has been added, false otherwise.
  #
  # @return [Boolean] Whether or not one or more errors have been added.
  def errors_found?
    errors.size > 0
  end

  # Returns true if one or more warnings has been added, false otherwise.
  #
  # @return [Boolean] Whether or not one or more errors have been added.
  def warnings_found?
    warnings.size > 0
  end

  # Iterates over and yields each error. If no block is given, returns
  # an +Enumerator+.
  #
  # @yield [error, options] each consecutive error and options hash.
  # @yieldparam warning [Exception] the error
  # @yieldparam options [Hash] the hash of options associated with +error+
  # @return [nil, Enumerator] +nil+ if no block is given, an +Enumerator+
  #   otherwise.
  def each_error(&block)
    if block_given?
      errors.each do |error_hash|
        yield error_hash[:error], error_hash[:options]
      end
    else
      to_enum(__method__)
    end
  end

  # Iterates over and yields each warning. If no block is given, returns
  # an +Enumerator+.
  #
  # @yield [warning, options] each consecutive warning and options hash.
  # @yieldparam warning [Exception] the warning
  # @yieldparam options [Hash] the hash of options associated with +warning+
  # @return [nil, Enumerator] +nil+ if no block is given, an +Enumerator+
  def each_warning(&block)
    if block_given?
      warnings.each do |warning_hash|
        yield warning_hash[:error], warning_hash[:options]
      end
    else
      to_enum(__method__)
    end
  end
end

Instance Method Details

#each_error {|error, options| ... } ⇒ nil, Enumerator

Iterates over and yields each error. If no block is given, returns an Enumerator.

Yields:

  • (error, options)

    each consecutive error and options hash.

Yield Parameters:

  • warning (Exception)

    the error

  • options (Hash)

    the hash of options associated with error

Returns:

  • (nil, Enumerator)

    nil if no block is given, an Enumerator otherwise.


67
68
69
70
71
72
73
74
75
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 67

def each_error(&block)
  if block_given?
    errors.each do |error_hash|
      yield error_hash[:error], error_hash[:options]
    end
  else
    to_enum(__method__)
  end
end

#each_warning {|warning, options| ... } ⇒ nil, Enumerator

Iterates over and yields each warning. If no block is given, returns an Enumerator.

Yields:

  • (warning, options)

    each consecutive warning and options hash.

Yield Parameters:

  • warning (Exception)

    the warning

  • options (Hash)

    the hash of options associated with warning

Returns:

  • (nil, Enumerator)

    nil if no block is given, an Enumerator


84
85
86
87
88
89
90
91
92
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 84

def each_warning(&block)
  if block_given?
    warnings.each do |warning_hash|
      yield warning_hash[:error], warning_hash[:options]
    end
  else
    to_enum(__method__)
  end
end

#errors_found?Boolean

Returns true if one or more errors has been added, false otherwise.

Returns:

  • (Boolean)

    Whether or not one or more errors have been added.


48
49
50
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 48

def errors_found?
  errors.size > 0
end

#report_error(error, options = {}) ⇒ void

This method returns an undefined value.

Add an error object to the list of collected errors.

Parameters:

  • error (Exception)

    The error object to add.

  • options (Hash) (defaults to: {})

    A hash of options to record with the error.


24
25
26
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 24

def report_error(error, options = {})
  errors << { error: error, options: options }
end

#report_warning(error, options = {}) ⇒ void

This method returns an undefined value.

Add an error object to the list of collected warnings.

Parameters:

  • error (Exception)

    The error object to add.

  • options (Hash) (defaults to: {})

    A hash of options to record with the error.


33
34
35
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 33

def report_warning(error, options = {})
  warnings << { error: error, options: options }
end

#resetvoid

This method returns an undefined value.

Clears all errors and warnings


40
41
42
43
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 40

def reset
  @errors = []
  @warnings = []
end

#warnings_found?Boolean

Returns true if one or more warnings has been added, false otherwise.

Returns:

  • (Boolean)

    Whether or not one or more errors have been added.


55
56
57
# File 'lib/rosette/core/error_reporters/buffered_error_reporter.rb', line 55

def warnings_found?
  warnings.size > 0
end