Class: JSONAPIHelpers::Serializers::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jsonapi_helpers/serializers/errors.rb

Instance Method Summary collapse

Constructor Details

#initializeErrors

Returns a new instance of Errors.



8
9
10
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 8

def initialize
  @errors = []
end

Instance Method Details

#add(**args) ⇒ Object



12
13
14
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 12

def add(**args)
  @errors << Error.new(**args)
end

#each(&block) ⇒ Object



16
17
18
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 16

def each(&block)
  @errors.each(&block)
end

#lengthObject Also known as: size



20
21
22
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 20

def length
  @errors.length
end

#to_hObject



25
26
27
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 25

def to_h
  { errors: @errors.map(&:to_h) }
end

#to_json(_context = nil) ⇒ Object

Rails is awkward and calls #to_json with a context argument NOTE: Rails only method Hash#to_json



31
32
33
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 31

def to_json(_context = nil)
  to_h.to_json
end