Class: JSONAPIHelpers::Serializers::Errors
- Inherits:
-
Object
- Object
- JSONAPIHelpers::Serializers::Errors
- Includes:
- Enumerable
- Defined in:
- lib/jsonapi_helpers/serializers/errors.rb
Instance Method Summary collapse
- #add(**args) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
- #length ⇒ Object (also: #size)
- #to_h ⇒ Object
-
#to_json(_context = nil) ⇒ Object
Rails is awkward and calls #to_json with a context argument NOTE: Rails only method Hash#to_json.
Constructor Details
#initialize ⇒ Errors
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 |
#length ⇒ Object Also known as: size
20 21 22 |
# File 'lib/jsonapi_helpers/serializers/errors.rb', line 20 def length @errors.length end |
#to_h ⇒ Object
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 |