Sha256: 5e51122133fc70a0d340570b22cc4fe03c5ffa4b20f5b043a61a26032e8e3464

Contents?: true

Size: 650 Bytes

Versions: 1

Compression:

Stored size: 650 Bytes

Contents

# frozen_string_literal: true

module JSONAPIHelpers
  module Serializers
    class Errors
      include Enumerable

      def initialize
        @errors = []
      end

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

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

      def length
        @errors.length
      end
      alias_method :size, :length

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

      # Rails is awkward and calls #to_json with a context argument
      # NOTE: Rails only method Hash#to_json
      def to_json(_context = nil)
        to_h.to_json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapi_helpers-0.2.0 lib/jsonapi_helpers/serializers/errors.rb