aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/nested_error.rb
blob: 93348c77716ed4fc473759c7bad0f794dbd99c6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

require "active_model/error"
require "forwardable"

module ActiveModel
  # Represents one single error
  # @!attribute [r] base
  #   @return [ActiveModel::Base] the object which the error belongs to
  # @!attribute [r] attribute
  #   @return [Symbol] attribute of the object which the error belongs to
  # @!attribute [r] type
  #   @return [Symbol] error's type
  # @!attribute [r] options
  #   @return [Hash] additional options
  # @!attribute [r] inner_error
  #   @return [Error] inner error
  class NestedError < Error
    def initialize(base, inner_error, override_options = {})
      @base = base
      @inner_error = inner_error
      @attribute = override_options.fetch(:attribute) { inner_error.attribute }
      @type = override_options.fetch(:type) { inner_error.type }
      @raw_type = inner_error.raw_type
      @options = inner_error.options
    end

    attr_reader :inner_error

    extend Forwardable
    def_delegators :@inner_error, :message
  end
end