Sha256: 49b42a321881b391a2faba2ac554469296712454923310958aa0e8d7ed627c2c

Contents?: true

Size: 1.54 KB

Versions: 25

Compression:

Stored size: 1.54 KB

Contents

require 'active_support/core_ext/module/delegation'

module JSONAPIonify::Structure
  module Helpers
    class Errors
      include Enumerable

      delegate :clear, :present?, :blank?, :each, to: :@hash

      def initialize
        @hash = {}
      end

      def initialize_copy(new_copy)
        super
        instance_variables.each do |ivar|
          new_copy.instance_variable_set(
            ivar, instance_variable_get(ivar).dup
          ) rescue nil
        end
      end

      def add(key, value)
        (@hash[key] ||= []) << value
      end

      alias_method :[]=, :add

      def [](key)
        @hash[key]
      end

      def replace(key, messages)
        @hash[key] = Array.wrap(messages)
      end

      def all_messages
        @hash.each_with_object([]) do |(key, errors), messages|
          errors.each do |error|
            messages << [backtick_key(key), error].join(' ')
          end
        end
      end

      def as_collection
        each_with_object(Collections::Errors.new) do |(pointer, messages), collection|
          *, key = pointer.to_s.split('/')
          entity = key.present? ? key : 'object'
          messages.each do |message|
            message = "#{entity} #{message}".chomp('.') << '.'
            object  = {
              source: {
                pointer: pointer
              },
              detail: message,
              status: "422"
            }
            collection << object
          end
        end
      end

      private

      def backtick_key(key)
        "`#{key}`"
      end

    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.9 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.8 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.7 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.5 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.4 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.3 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.2 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.1 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.12.0 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.11 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.10 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.9 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.8 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.7 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.6 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.5 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.11.0 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.10.2 lib/jsonapionify/structure/helpers/errors.rb
jsonapionify-0.10.1 lib/jsonapionify/structure/helpers/errors.rb