Sha256: f67f0e90ee768f2ebdaae821b09affbbba9c7ad1e23f737cc24eef59b6ec3b4f

Contents?: true

Size: 1.61 KB

Versions: 24

Compression:

Stored size: 1.61 KB

Contents

require 'active_support/core_ext/module/delegation'

module JSONAPIonify
  module Api
    class ErrorsObject

      delegate :present?, to: :collection

      class Evaluator
        Structure::Objects::Error.permitted_keys.each do |key|
          define_method(key) do |value|
            @error[key] = value
          end
        end

        def initialize(error)
          @error = error
          freeze
        end

        def meta
          JSONAPIonify::Structure::Helpers::MetaDelegate.new @error
        end

        def pointer(value)
          @error[:source]           ||= {}
          @error[:source][:pointer] = value
        end

        def parameter(value)
          @error[:source]             ||= {}
          @error[:source][:parameter] = value
        end
      end

      def evaluate(*args, error_block:, runtime_block: nil, backtrace: nil)
        backtrace     ||= caller
        runtime_block ||= proc {}
        error         = Structure::Objects::Error.new
        evaluator     = Evaluator.new(error)
        collection << error
        [runtime_block, error_block].each do |block|
          evaluator.instance_exec(*args, &block) if block
        end
        if JSONAPIonify.show_backtrace == true
          error[:meta]             ||= {}
          error[:meta][:backtrace] = backtrace
        end
      end

      def top_level
        JSONAPIonify.new_object.tap do |obj|
          obj[:errors] = collection
        end
      end

      def collection
        @collection ||= Structure::Collections::Errors.new
      end

      def set(collection)
        @collection = collection
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

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