Class: Foobara::ErrorCollection

Inherits:
Array
  • Object
show all
Defined in:
foobara-0.1.7/projects/common/src/error_collection.rb

Defined Under Namespace

Classes: ErrorAlreadySetError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_h(errors) ⇒ Object



6
7
8
9
10
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 6

def to_h(errors)
  new.tap do |collection|
    collection.add_errors(errors)
  end.errors_hash
end

Instance Method Details

#add_error(error_or_collection_or_error_hash) ⇒ Object



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
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 52

def add_error(error_or_collection_or_error_hash)
  error = case error_or_collection_or_error_hash
          when Error
            error_or_collection_or_error_hash
          when Hash
            if error_or_collection_or_error_hash.key?(:symbol) &&
               error_or_collection_or_error_hash.key?(:message)
              Error.new(**error_or_collection_or_error_hash)
            else
              # :nocov:
              raise ArgumentError,
                    "if passing a hash of error args it must include symbol and message at least"
              # :nocov:
            end
          else
            # :nocov:
            raise ArgumentError, "Not sure how to convert #{error_or_collection_or_error_hash.inspect} " \
                                 "into an error. Can handle a hash of error " \
                                 "args or 3 arguments for symbol, message, and context, or, of course, an Error"
            # :nocov:
          end

  if has_error?(error)
    raise ErrorAlreadySetError, "cannot set #{error} more than once"
  end

  self << error
end

#add_errors(errors) ⇒ Object



81
82
83
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 81

def add_errors(errors)
  Util.array(errors).each { |error| add_error(error) }
end

#each_errorObject



35
36
37
38
39
40
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 35

def each_error(&)
  # :nocov:
  warn "DEPRECATED: This method will be deprecated in the coming version"
  each(&)
  # :nocov:
end

#error_arrayObject



28
29
30
31
32
33
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 28

def error_array
  # :nocov:
  warn "DEPRECATED: Do not call ErrorCollection#error_array instead just use the collection directly."
  self
  # :nocov:
end

#errorsObject



21
22
23
24
25
26
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 21

def errors
  # :nocov:
  warn "DEPRECATED: Do not call ErrorCollection#errors instead just use the collection directly."
  self
  # :nocov:
end

#errors_hashObject



85
86
87
88
89
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 85

def errors_hash
  each_with_object({}) do |error, hash|
    hash[error.key] = error.to_h
  end
end

#has_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 42

def has_error?(error)
  unless error.is_a?(Error)
    # :nocov:
    raise ArgumentError, "Can only check if an Error instance is in the collection"
    # :nocov:
  end

  include?(error)
end

#has_errors?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 17

def has_errors?
  !empty?
end

#keysObject



95
96
97
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 95

def keys
  map(&:key)
end

#success?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 13

def success?
  empty?
end

#to_hObject



99
100
101
102
103
104
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 99

def to_h
  # :nocov:
  warn "DEPRECATED: Use #errors_hash instead"
  errors_hash
  # :nocov:
end

#to_sentenceObject



91
92
93
# File 'foobara-0.1.7/projects/common/src/error_collection.rb', line 91

def to_sentence
  Util.to_sentence(map(&:message))
end