Class: SoberSwag::Reporting::Report::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sober_swag/reporting/report/base.rb

Overview

Base class for SoberSwag reports.

These reports are what make these serializers and parsers reporting: they provide errors. For outputs, these are errors encountered during serialization, IE, places where we lied about what type we were going to serialize. This is mostly used for testing.

For parsers, these are encountered during parsing. This can be easily converted into a hash of JSON path objects to individual errors, enabling developers to more easily see what's gone wrong.

Direct Known Subclasses

Either, List, MergedObject, Object, Value

Instance Method Summary collapse

Instance Method Details

#each_error {|path, val| ... } ⇒ Object #each_errorEnumerable<String, String>

Overloads:

  • #each_error {|path, val| ... } ⇒ Object

    Yields each error to the block.

    Yields:

    • (path, val)

      the JSON path to the error, and an error string

    Yield Parameters:

    • (String, String)
  • #each_errorEnumerable<String, String>

    Returns an enum of two values: error keys and error values. Note: the same key can potentially occur more than once!.

    Returns:

    • (Enumerable<String, String>)

      an enum of two values: error keys and error values. Note: the same key can potentially occur more than once!



51
52
53
# File 'lib/sober_swag/reporting/report/base.rb', line 51

def each_error
  return enum_for(:each_error) unless block_given?
end

#full_errorsArray<[String]>

Returns An array of error paths and error components, in the form of:

  [
    'foo.bar: was bad',
    'foo.bar: was even worse'
  ]
```.

Returns:

  • (Array<[String]>)

    An array of error paths and error components, in the form of:

      [
        'foo.bar: was bad',
        'foo.bar: was even worse'
      ]
    


26
27
28
29
30
# File 'lib/sober_swag/reporting/report/base.rb', line 26

def full_errors
  each_error.map do |k, v|
    [k, v].reject(&:blank?).join(': ')
  end
end

#path_hashHash<String,Array<String>>

Get a hash where each key is a JSON path, and each value is an array of errors for that path.

Returns:

  • (Hash<String,Array<String>>)

    hash of JSON path to errors



35
36
37
38
39
40
41
# File 'lib/sober_swag/reporting/report/base.rb', line 35

def path_hash
  Hash.new { |h, k| h[k] = [] }.tap do |hash|
    each_error do |k, v|
      hash["$#{k}"] << v
    end
  end
end